1102. Invert a Binary Tree (25)

来源:互联网 发布:阿里云怎么代理加盟 编辑:程序博客网 时间:2024/06/03 21:11

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

#include <bits/stdc++.h>using namespace std;const int maxn = 100;struct node {    int data, lchild, rchild;    node(): data(-1), lchild(-1), rchild(-1){}}nodes[maxn];bool isNode[maxn] = { true };int n;void init() {    for (int i = 0; i < maxn; i++) {        isNode[i] = true;    }}void LevelOrderTraversal(int root) {    queue<node> q;    q.push(nodes[root]);    int cnt = 0;    while (!q.empty()) {        node kfront = q.front();        cnt++;        printf("%d%c", kfront.data, (cnt == n) ? '\n':' ');        q.pop();        if (kfront.rchild != -1) {            q.push(nodes[kfront.rchild]);        }        if (kfront.lchild != -1) {            q.push(nodes[kfront.lchild]);        }    }}void InOrderTraversal(int root) {    int cnt = 0;    stack<node> s;    node p = nodes[root];    node temp;    while (p.data != -1 || !s.empty()) {        if (p.data != -1) {            temp = p;            s.push(temp);            if (p.rchild != -1) p = nodes[p.rchild];            else p.data = -1;        }        else {            if (!s.empty()) {                temp = s.top();                s.pop();                p = temp;                cnt++;                printf("%d%c", p.data, (cnt == n) ? '\n':' ');                if (p.lchild != -1) p = nodes[p.lchild];                else p.data = -1;            }        }    }}int main(){    init();    scanf("%d", &n);    char left[3], right[3];    for (int i = 0; i < n; i++) {        scanf("%s %s", left, right);        if (left[0] != '-') {            nodes[i].lchild = atoi(left);            isNode[atoi(left)] = false;        }        if (right[0] != '-') {            nodes[i].rchild = atoi(right);            isNode[atoi(right)] = false;        }    }    int root;    for (int i = 0; i < n; i++) {        if (isNode[i] == true) root = i;    }    for (int i = 0; i < n; i++) {        nodes[i].data = i;    }    LevelOrderTraversal(root);    InOrderTraversal(root);    return 0;}