PAT1102. Invert a Binary Tree

来源:互联网 发布:web前端编程自学难吗 编辑:程序博客网 时间:2024/06/08 11:10

题目描述
给定一颗二叉树,要求你将其翻转,即每一个节点的左右子树互换。最后将翻转之后的二叉树以层次遍历和中序遍历做输出。例如:
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

解题思路
    首先是二叉树的存储,由于输入的信息中包含了每一个节点的子节点以及父节点,所以我们新建一个数据结构来保存每一个节点的信息。
    接着是对二叉树的翻转,这其实可以在输入的时候将左子树的数据保存在右子树,将右子树的数据保存在左子树。
    最后遍历输出结果,首先是找到根节点,接着根据该节点遍历二叉树。层次遍历,利用STL提供的queue队列一层一层的遍历。中序遍历,利用递归的方法实现。

实现代码

/************************************************************************* *  > File Name: pat1102.cpp *  > Author: ZQKC *  > Mail: 1979408672@qq.com *  > Created Time: Mon 01 Feb 2016 01:07:04 PM CST ************************************************************************/#include <iostream>#include <cstring>#include <queue>#include <cstdio>//二叉树节点结构typedef struct Node{        int parent;        int child[2]; //child[0]是左孩子}Node;//存储二叉树Node arr[10];//中序遍历打印结果void in_order(int root, bool &tag){        if ( arr[root].child[0]!=-1 )        {                in_order( arr[root].child[0], tag);        }        if (tag)        {                printf("%d",root);                tag = false;        }        else        {                printf (" %d",root);        }        if ( arr[root].child[1]!=-1 )        {                in_order( arr[root].child[1], tag);        }}int main(){        int N;        char ch[2];        while( ~scanf("%d",&N) )        {                getchar();                for (int i=0; i<N; ++i)                        arr[i].parent = arr[i].child[0] = arr[i].child[1] = -1;                for (int i=0; i<N; ++i)                {                        scanf("%c %c",&ch[0], &ch[1]);                        getchar();                        for (int j=0; j<2; ++j)                        {                                if ( ch[j]!='-' )                                {                                        arr[i].child[ (j+1)%2 ] = ch[j]-'0';                                        arr[ ch[j]-'0' ].parent = i;                                }                        }                }                //寻找根节点                int root;                for (int i=0; i<N; ++i)                {                        if (arr[i].parent==-1)                        {                                root = i;                                break;                        }                }                //层次遍历                std::queue<int> que;                que.push( root );                printf("%d", root);                while ( !que.empty() )                {                        int n = que.front();                        que.pop();                        for (int i=0; i<2; ++i)                        {                                if ( arr[n].child[i] != -1 )                                {                                        printf (" %d",arr[n].child[i]);                                        que.push( arr[n].child[i] );                                }                        }                }                printf("\n");                bool tag = true;                in_order(root, tag);                printf("\n");        }        return 0;}
0 0