PAT 1102. Invert a Binary Tree (25)

来源:互联网 发布:centos iscsi 编辑:程序博客网 时间:2024/05/29 19:32

1102. Invert a Binary Tree (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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:
81 -- -0 -2 7- -- -5 -4 6
Sample Output:
3 7 2 6 4 0 5 16 5 7 4 3 2 0 1

提交代码


提示: 输入中未出现的编号为根结点, 从根节点开始建树。注意建树时左孩子为右端字符。AC代码:


#include <bits/stdc++.h>#define PI acos(-1)#define INF 0x3f3f3f3f#define SCD(a) scanf("%d",&a)#define SCDD(a,b) scanf("%d%d",&a,&b)#define SCF(a) scanf("%lf",&a)#define PTD(a) printf("%d", a)#define PTDE(a) printf("%d\n",a)#define MST(a,b) memset(a, b, sizeof(a))typedef long long LL;using namespace std;const int M = 510;int n, m;bool use[M], flag;struct w{    int l;    int r;};w a[M];struct BSTNode{    int data;    BSTNode* lchild;    BSTNode* rchild;};BSTNode* T;void init(){    MST(use, false);}void CreateBSTree(BSTNode* &T, int val){    if(!T){        T = new BSTNode;        T->data = val;        T->lchild = NULL;        T->rchild = NULL;    }    if( a[val].l != -1 )        CreateBSTree(T->rchild, a[val].l);    if( a[val].r != -1 )        CreateBSTree(T->lchild, a[val].r);}void LevelTraverse(){    queue<BSTNode*> q;    BSTNode* p;    p = T;    q.push(p);    flag = true;    while(!q.empty()){        p = q.front();        q.pop();        if(flag){            flag = false;        }else{            cout<<" ";        }        cout<<p->data;        if(p->lchild)            q.push(p->lchild);        if(p->rchild)            q.push(p->rchild);    }    cout<<endl;    flag = true;}void InOrderTraverse(BSTNode* &T){    if(!T)        return;    if(T->lchild)        InOrderTraverse(T->lchild);    if(flag)        flag = false;    else        cout<<" ";    cout<<T->data;    if(T->rchild)        InOrderTraverse(T->rchild);}void solve(){    int i;    char c;    for(i=0; i<n; i++){        cin>>c;        if(c == '-'){            a[i].l = -1;        }else{            a[i].l = c - 48;            use[ a[i].l ] = true;        }        cin>>c;        if(c == '-'){            a[i].r = -1;        }else{            a[i].r = c - 48;            use[ a[i].r ] = true;        }    }    for(i=0; i<n; i++){        if(!use[i]){            CreateBSTree(T, i);            break;        }    }}int main(){    int i ,j;    SCD(n);    init();    solve();    LevelTraverse();    InOrderTraverse(T);    cout<<endl;    return 0;}




原创粉丝点击