PAT1102. Invert a Binary Tree

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值