PAT--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 (≤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 <iostream>
#include <bits/stdc++.h>

using namespace std;

struct TreeNode{
    int data;
    int left,right;
}t[15];
int n;

int inTravel[15]; //中序遍历结果存储数组
int flag1 = 0; //中序遍历标志位

int child(char x){
    if(x=='-'){
            return -1;
        }else{
            return x-'0';
        }
}

//中序遍历
void inOrder(int root){
    if(root==-1) return;  
    if(t[root].left!=-1)
        inOrder(t[root].left);
    inTravel[flag1++] = root;
    if(t[root].right!=-1)
        inOrder(t[root].right);
}

int main()
{
    cin>>n;
    char l,r; 
    int father[n];  //用于并查集寻找根节点
    int layerTrave[n]; //层序遍历的结果存储
    int flag = 0;  //层序遍历标志位
    
    memset(father,-1,sizeof(father));//初始化父亲结点数组
    
    for(int i=0;i<n;i++){
        cin>>l>>r;
        t[i].data = i;
        t[i].left = child(r);
        t[i].right = child(l);
        if(t[i].left>=0){
            father[child(r)] = i;
        }
        if(t[i].right>=0){
            father[child(l)] = i;
        }
    }    //输入过程,相当于建树过程

    
    //查找根节点
    int x = father[0];  //可以从任一结点开始查找,因为在一棵树上,所以总会找到根节点
    int root = 0;   //用来存放根节点
    while(x!=-1){
        root = x;
        x = father[x];  //x相当于结点的父结点
    }
    
    //层序遍历过程
    queue <TreeNode> q;
    q.push(t[root]);
    while(!q.empty()){
        TreeNode tmp = q.front();
        layerTrave[flag++] = tmp.data;
        q.pop();
        if(tmp.left!=-1)
            q.push(t[tmp.left]);
        if(tmp.right!=-1){
            q.push(t[tmp.right]);
        }
    }

    for(int i=0;i<flag;i++){
        if(i==0)
            cout<<layerTrave[i];
        else
            cout<<" "<<layerTrave[i];
    }
    cout<<endl;

    //中序遍历
    inOrder(root);

    for(int i=0;i<flag1;i++){
        if(i==0)
            cout<<inTravel[i];
        else
            cout<<" "<<inTravel[i];
    }
    cout<<endl;
    return 0;
}

 分析:

建树的时候就直接逆转建树,这样就可以按照正常顺序进行层序遍历和中序遍历。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值