The following is from Max Howell @twitter:node
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!函數
Each input file contains one test case. For each case, the first line gives a positive integerN(≤10) which is the total number of nodes in the tree -- and hence the nodes are numbered from 0 toN−1. ThenNlines follow, each corresponds to a node from 0 toN−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.post
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.spa
8 1 - - - 0 - 2 7 - - - - 5 - 4 6
3 7 2 6 4 0 5 1 6 5 7 4 3 2 0 1
#include <cstdio> #include <queue> using namespace std; const int maxn = 20; struct node { int data; int lchild, rchild; }Node[maxn]; int n; bool noRoot[maxn]; char c1, c2; // 反轉的後序遍歷 int num = 0; void postOrder(int root) { if (root == -1) return; postOrder(Node[root].rchild); printf("%d", Node[root].data); if (num < n - 1) printf(" "); num ++; postOrder(Node[root].lchild); } // 反轉後的層次遍歷 int num1 = 0; void layerOrder(int root) { if (root == -1) return; queue<int> q; q.push(root); while (!q.empty()) { int now = q.front(); q.pop(); printf("%d", Node[now].data); if (num1 < n - 1) { printf(" "); } num1 ++; if (Node[now].rchild != -1) q.push(Node[now].rchild); if (Node[now].lchild != -1) q.push(Node[now].lchild); } } int main() { //freopen("test.txt", "r", stdin); scanf("%d", &n); for (int i = 0; i < n; i ++) { getchar(); scanf("%c %c", &c1, &c2); Node[i].data = i; if (c1 != '-') { Node[i].lchild = c1 - '0'; noRoot[c1 - '0'] = true; } else { Node[i].lchild = -1; } if (c2 != '-') { Node[i].rchild = c2 - '0'; noRoot[c2 - '0'] = true; } else { Node[i].rchild = -1; } } int k; for (int i = 0; i < n; i ++) if (!noRoot[i]) k = i; layerOrder(k); printf("\n"); postOrder(k); return 0; }
#include <cstdio> #include <queue> #include <algorithm> using namespace std; const int maxn = 110; struct node { int lchild, rchild; }Node[maxn]; bool noRoot[maxn]; int n, num = 0; void print(int id) { printf("%d", id); num ++; if (num < n) printf(" "); else printf("\n"); } // 中序遍歷 void inOrder(int root) { if (root == -1) return; inOrder(Node[root].lchild); print(root); inOrder(Node[root].rchild); } // 層次遍歷 void layerOrder(int root) { queue<int> q; q.push(root); while (!q.empty()) { int now = q.front(); q.pop(); print(now); if (Node[now].lchild != -1) q.push(Node[now].lchild); if (Node[now].rchild != -1) q.push(Node[now].rchild); } } // 後序遍歷, 反轉二叉樹 void postOrder(int root) { if (root == -1) return; postOrder(Node[root].lchild); postOrder(Node[root].rchild); swap(Node[root].lchild, Node[root].rchild); //交換左右孩子 } int strToNum(char c) { if (c == '-') return -1; else { noRoot[c - '0'] = true; return c - '0'; } } int findRoot() { for (int i = 0; i < n; i ++) { if (noRoot[i] == false) return i; } } int main() { char lchild, rchild; scanf("%d", &n); for (int i = 0; i < n; i ++) { getchar(); scanf("%c %c", &lchild, &rchild); Node[i].lchild = strToNum(lchild); Node[i].rchild = strToNum(rchild); } int root = findRoot(); postOrder(root); layerOrder(root); num = 0; inOrder(root); return 0; }