日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

1102 Invert a Binary Tree(甲级)

發布時間:2024/7/23 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 1102 Invert a Binary Tree(甲级) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1102 Invert a Binary Tree (25分)
The following is from Max Howell @twitter:
Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so fuck off.
Now it’s your turn to prove that YOU CAN invert a binary tree!
Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤10) which is the total number of nodes in the tree – and hence the nodes are numbered from 0 to N?1. Then N lines follow, each corresponds to a node from 0 to N?1, and gives the indices of the left and right children of the node. If the child does not exist, a - will be put at the position. Any pair of children are separated by a space.
Output Specification:

For each test case, print in the first line the level-order, and then in the second line the in-order traversal sequences of the inverted tree. There must be exactly one space between any adjacent numbers, and no extra space at the end of the line.
Sample Input:

8
1 -

0 -
2 7

5 -
4 6
Sample Output:

3 7 2 6 4 0 5 1
6 5 7 4 3 2 0 1

思路:由于題目直接給出了節點編號關系,因此使用二叉樹的靜態寫法非常方便。處理輸入問題,如果是數字,直接賦值給該節點的孩子,否則不進行操作,默認為-1;建樹完成后可以使用先序遍歷或者是后序遍歷進行樹的反轉,改變原來樹的結構,隨后就是簡單的遍歷操作啦。

#include<iostream> #include<string> #include<vector> #include<queue> using namespace std; const int maxn = 100; struct node {//二叉樹靜態寫法int lchild=-1, rchild=-1; }no[maxn]; int n; int in_flag = 0, le_flag = 0;//在中序遍歷和層序遍歷時控制格式; void postorder(int root)//后序遍歷進行反轉 {if (root==-1){return;}postorder(no[root].lchild);postorder(no[root].rchild);swap(no[root].lchild, no[root].rchild);//交換左右孩子,反轉; } void inorder(int root)//中序遍歷二叉樹 {if (root == -1){return;}inorder(no[root].lchild);if (in_flag++ == 0)cout << root;else cout << " " <<root;inorder(no[root].rchild); } void level(int root)//層序遍歷二叉樹 {queue<int>q;q.push(root);int le_flag = 0;while (!q.empty()){int tmp = q.front();q.pop();if (le_flag++ == 0)cout << tmp;else cout << " " << tmp;if (no[tmp].lchild != -1)q.push(no[tmp].lchild);if (no[tmp].rchild != -1)q.push(no[tmp].rchild);}} int main() {int fa[11]{ 0 };//記錄每個節點是否有父節點cin >> n;for (int i = 0; i < n; i++)//建立靜態二叉樹{string str1, str2;cin >> str1 >> str2;if (str1 != "-"){no[i].lchild = stoi(str1);fa[no[i].lchild] = 1;}if (str2 != "-"){no[i].rchild = stoi(str2);fa[no[i].rchild] = 1;}}int i = 0;for (i; i < n; i++)//尋找根節點,根節點的父節點不存在{if (fa[i] == 0){break;}}postorder(i);level(i);cout << endl;inorder(i); }

也可以不對樹進行改變,因為題目只是需要正確的輸出就行,我們就可以只模擬輸出。

#include<iostream> #include<string> #include<vector> #include<queue> using namespace std; const int maxn = 100; struct node {//二叉樹靜態寫法int lchild=-1, rchild=-1; }no[maxn]; int n; int in_flag = 0, le_flag = 0;//在中序遍歷和層序遍歷時控制格式; void inorder(int root)//不進行節點交換,先遍歷右子樹 {if (root==-1){return;}inorder(no[root].rchild);if (in_flag++ == 0)cout << root;else cout << " " << root;inorder(no[root].lchild); } void level(int root)//層序遍歷二叉樹 {queue<int>q;q.push(root);int le_flag = 0;while (!q.empty()){int tmp = q.front();q.pop();if (le_flag++ == 0)cout << tmp;else cout << " " << tmp;if (no[tmp].rchild != -1)q.push(no[tmp].rchild);//從右往左pushif (no[tmp].lchild != -1)q.push(no[tmp].lchild);//}} int main() {int fa[11]{ 0 };//記錄每個節點是否有父節點cin >> n;for (int i = 0; i < n; i++)//建立靜態二叉樹{string str1, str2;cin >> str1 >> str2;if (str1 != "-"){no[i].lchild = stoi(str1);fa[no[i].lchild] = 1;}if (str2 != "-"){no[i].rchild = stoi(str2);fa[no[i].rchild] = 1;}}int i = 0;for (i; i < n; i++)//尋找根節點,根節點的父節點不存在{if (fa[i] == 0){break;}}level(i);cout << endl;inorder(i);}

總結

以上是生活随笔為你收集整理的1102 Invert a Binary Tree(甲级)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。