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<vector>
#include<queue>
#include<string>
using namespace std;

vector<vector<int> > vec;
bool visit[100] = {false};
struct node
{
    int data;
    node * lchild;
    node * rchild;
};

node * creat_tree(int start){

    if(start == -1){
        return NULL;
    }
    node * root = new node();
    root -> data = start;
    root -> lchild = creat_tree(vec[start][1]);
    root -> rchild = creat_tree(vec[start][0]);

    return root;
}
void level_order(node * t){

    queue<node *> que;
    que.push(t);
    while (!que.empty())
    {
        printf("%s%d", que.front() == t ? "" : " ",que.front() -> data);
        if(que.front() -> lchild != NULL){
            que.push(que.front() -> lchild);
        }
        if(que.front() -> rchild != NULL){
            que.push(que.front() -> rchild);
        }
        que.pop();
    }
    

}

node * tree;
int flag = 0;
void in_order(node *t){
    
    if(t == NULL){
        return;
    }

    in_order(t -> lchild);
    printf("%s%d", flag == 0 ? "" : " ", t -> data);
    flag++;
    in_order(t -> rchild);

}
int main(){
    
    int n;
    cin >> n;
    vec.resize(n);
    for(int i = 0; i < n; i++){

        string a, b;
        cin >> a >> b;
        if(a == "-"){
            vec[i].push_back(-1);

        }else
        {
            vec[i].push_back(stoi(a));
            visit[stoi(a)] = true;
            // cout << stoi(a) << endl;
        }
         
        if(b == "-"){
            vec[i].push_back(-1);
        }else
        {
            vec[i].push_back(stoi(b));
            visit[stoi(b)] = true;
            // cout << stoi(b) << endl;
        }
        

    }
    int start;
    for(int i = 0; i < n; i++){
        if(!visit[i]){
            start = i;
            break;
        }
    }
    // cout << start << endl;
    tree = creat_tree(start);
    level_order(tree);

    printf("\n");

    in_order(tree);
    return 0;
}
发布了86 篇原创文章 · 获赞 15 · 访问量 1111

猜你喜欢

转载自blog.csdn.net/zbchenchanghao/article/details/104175264