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 (≤) which is the total number of nodes in the tree -- and hence the nodes are numbered from 0 to N1. Then N lines follow, each corresponds to a node from 0 to N1, 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=1010;
#define  inf  0x3fffffff
struct node{
    int lchild=-1;
    int rchild=-1;
}node[maxn];
vector<int> level,in;
vector<int> flag;
void leorder(int root){
    queue<int> q;
    q.push(root);
    while(!q.empty()){
        int t=q.front();
        q.pop();
        level.push_back(t);
        if(node[t].lchild!=-1){
            q.push(node[t].lchild);
        }
        if(node[t].rchild!=-1){
            q.push(node[t].rchild);
        }
    }
}
void inorder(int root){
    if(root==-1){
        return ;
    }
    inorder(node[root].lchild);
    in.push_back(root);
    inorder(node[root].rchild);
}
int main(){
    int n;
    scanf("%d",&n);
    char a,b;
    int a1,b1;
    for(int i=0;i<n;i++){
        scanf("%*c%c %c",&a,&b);
        if(a>='0'&&a<='9'){
            a1=a-'0';
            node[i].lchild=a1;
        }
        else{
            node[i].lchild=-1;
        }
        if(b>='0'&&b<='9'){
            b1=b-'0';
            node[i].rchild=b1;
        }
        else{
            node[i].rchild=-1;
        }
        int temp;
        temp=node[i].lchild;
        node[i].lchild=node[i].rchild;
        node[i].rchild=temp;
    }
    flag.resize(n);
    for(int i=0;i<n;i++){
        if(node[i].lchild>=0)//一定要判断,不然会报运行时错误,找了好久才发现
        flag[node[i].lchild]=1;
        if(node[i].rchild>=0)
        flag[node[i].rchild]=1;
    }
    int root;
    for(int i=0;i<n;i++){
        if(flag[i]!=1){
            root=i;
            break;
        }
    }
    leorder(root);
    for(int i=0;i<level.size();i++){
        if(i<level.size()-1){
            printf("%d ",level[i]);
        }
        else{
            printf("%d
",level[i]);
        }
    }

    inorder(root);
    for(int i=0;i<in.size();i++){
        if(i<in.size()-1){
            printf("%d ",in[i]);
        }
        else{
            printf("%d
",in[i]);
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/dreamzj/p/14442775.html