1.和leetcode中的Invert Binary Tree(easy)题目一样。
2.利用递归函数,每次都把左右子树翻转即可。
翻转函数如下:
void invertTree(TreeNode* root) { if (root != NULL) { swap(root->l, root->r); invertTree(root->l); invertTree(root->r); } }
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
AC代码:
//#include<string> //#include <iomanip> #include<vector> #include <algorithm> //#include<stack> #include<set> #include<queue> #include<map> //#include<unordered_set> #include<unordered_map> //#include <sstream> //#include "func.h" //#include <list> #include<stdio.h> #include<iostream> #include<string> #include<memory.h> #include<limits.h> using namespace std; struct TreeNode{ int val; TreeNode*l, *r; TreeNode() :val(-1),l(NULL), r(NULL){}; }; void invertTree(TreeNode* root) { if (root != NULL) { swap(root->l, root->r); invertTree(root->l); invertTree(root->r); } } void levelOrder(TreeNode*root,vector<int>&ans) { queue<TreeNode*> q; if (root != NULL) { q.push(root); int count1 = 1; int count2 = 0; while (!q.empty()) { for (int i = 0; i < count1; i++) { TreeNode* head = q.front(); q.pop(); ans.push_back(head->val); if (head->l != NULL) { q.push(head->l); count2++; } if (head->r != NULL) { q.push(head->r); count2++; } } count1 = count2; count2 = 0; } } } void inOrder(TreeNode*root, vector<int>&ans) { if (root != NULL) { inOrder(root->l, ans); ans.push_back(root->val); inOrder(root->r, ans); } } int main(void) { int sum; cin >> sum; vector<TreeNode> tree(sum); vector<int> degree(sum,0); for (int i = 0; i < sum; i++) { tree[i].val = i; char a, b; cin >> a >> b; if (a!='-') { tree[i].l = &tree[a - '0']; degree[a - '0']++; } if (b != '-') { tree[i].r = &tree[b - '0']; degree[b - '0']++; } } TreeNode* root=NULL; for (int i = 0; i < sum; i++) { if (degree[i] == 0) { root = &tree[i]; break; } } invertTree(root); vector<int> ans1(0); vector<int> ans2(0); levelOrder(root, ans1); inOrder(root, ans2); for (int i = 0; i < ans1.size(); i++) { cout << ans1[i]; if (i != ans1.size() - 1) cout << " "; } cout << endl; for (int i = 0; i < ans2.size(); i++) { cout << ans2[i]; if (i != ans2.size() - 1) cout << " "; } cout << endl; return 0; }