1102. Invert a Binary Tree (25)

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 <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <queue>

using namespace std;

#define N 20

int n ;

struct data
{
    int left ;
    int right ;
};

data dt[N] ;

int fa[N] ;

int find(int x)
{
    if(x == fa[x])
        return x;
    return fa[x] = find(fa[x]) ;
}

void merg(int x , int y)
{
    fa[find(y)] = find(x);
}


typedef struct node{
    int data;
    struct node* left;
    struct node* right ;
    node(int _data = - 1)
    {
        data = _data ;
    }
}Bnode ;


Bnode* createTree(Bnode* root , int num)
{
    root = new node(num);
    if(dt[num].left == -1)
        root->left = NULL ;
    else
        root->left = createTree( root->left , dt[num].left);

    if(dt[num].right == -1)
        root->right = NULL ;
    else
        root->right = createTree( root->right , dt[num].right);

    return root ;
}


void levelorder(Bnode* root)
{
    queue<Bnode*> que;
    que.push(root) ;
    bool flag = false;

    while( !que.empty())
    {
        Bnode* bt = que.front();
        que.pop();
        if(flag == false)
        {
            printf("%d",bt->data);
            flag = true;
        }else{
            printf(" %d",bt->data);
        }
        if(bt->right != NULL)
        {
            que.push(bt->right);
        }
        if(bt->left != NULL)
        {
            que.push(bt->left);
        }
    }
    printf("\n");
}

Bnode* reverse(Bnode* root)
{
    if(root == NULL)
        return NULL;
    reverse(root->left);
    reverse(root->right);
    Bnode* tmp = root->left ;
    root->left = root->right;
    root->right = tmp ;
    return root ;
}


vector<int> in;

void inorder(Bnode* root2)
{
    if(root2 != NULL)
    {
        inorder(root2->left);
        in.push_back(root2->data);
        inorder(root2->right);
    }
}

int main()
{
    //freopen("in.txt","r",stdin);
    scanf("%d\n",&n);
    int i ;
    char a , b ;
    for(i = 0 ; i< n ;i++)
    {
        fa[i] = i ;
    }
    for(i = 0 ; i < n ;i++)
    {
        scanf("%c %c\n",&a,&b);
        if(a == '-')
        {
            dt[i].left = -1 ;
        }else{
            dt[i].left = a  - '0' ;
            if(find(i) != find(dt[i].left))
                merg(i,dt[i].left);
        }
        if(b == '-')
        {
            dt[i].right = -1 ;
        }else{
            dt[i].right = b - '0' ;
            if(find(i) != find(dt[i].right))
                merg(i,dt[i].right);
        }
    }

    int rootNum = find(0) ;

    Bnode* root = NULL;

    root = createTree( root , rootNum);

    levelorder(root);

    Bnode* root2 = reverse(root);
    in.clear();
    inorder(root2);

    printf("%d",in[0]);
    for(i = 1;i<n;i++)
    {
        printf(" %d",in[i]);
    }
    printf("\n");
    return 0 ;
}

你可能感兴趣的:(1102. Invert a Binary Tree (25))