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

题意:
二叉树有N个结点,给出每个结点的孩子结点的编号,把二叉树反转,输出反转后二叉树的层次遍历序列和中序遍历序列。

样例解释:
8 表示一个八个节点
1 - 0号结点的左孩子是1,没有右孩子

思路:
1.题目给出的是结点编号的关系,因此静态二叉树会比较方便。
2.输入的时候,如果是"-"表示没有孩子结点,用-1表示,如果是数字,直接把lchild和rchild记为该数字就可以。同时,需要找到这个二叉树的根结点,找一个结点,它不是任何结点的孩子即可。开一个bool型的notRoot数组,在输入时看看哪个结点没有作为其他结点的孩子出现,若出现则置为true,表示它不是根结点。然后遍历notRoot数组,若notRoot[i]==false即表示i是根结点。
3.二叉树反转可以通过后序遍历实现(在后序遍历访问根结点时交换lchild和rchild)

注意:
scanf的%c格式会读入换行符,所以需要在每行输入前把上一行的换行符吸收掉,**可以使用getchar(),或者用scanf("%c "),用%c就可以在scanf中接收一个字符。

#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn = 110;
struct node
{
    int lchild;
    int rchild;
}Node[maxn];
bool notRoot[maxn] = {false};   //初始化,都记为根结点
int n, num = 0;     //结点个数,已输出结点的个数


void print(int id)  //输出结点id的编号
{
    printf("%d", id);   //输出id
    num++;
    if(num < n)
        printf(" ");    //最后一个结点不输出空格
    else
        printf("\n");   //全部输出完后换行
}

void inOrder(int root)      //中序遍历
{
    if(root == -1)  return;
    inOrder(Node[root].lchild);
    print(root);
    inOrder(Node[root].rchild);
}


//层次遍历
void BFS(int root)
{
    queue<int> q;   //注意队列里存的是地址
    q.push(root);   //根结点地址入队
    while(!q.empty())
    {
        int top = q.front();    //取出队首元素
        q.pop();
        print(top);     //输出队首元素(树的总根结点)
        if(Node[top].lchild != -1)      //当前结点的左子树非空,注意里面是top不是root(因为是当前结点,也就是队首结点top)
            q.push(Node[top].lchild);
        if(Node[top].rchild != -1)      //当前结点的右子树非空
            q.push(Node[top].rchild);
    }
}

void postOrder(int root)    //后序遍历,用来反转二叉树
{
    if(root == -1)  return;
    postOrder(Node[root].lchild);
    postOrder(Node[root].rchild);
    swap(Node[root].lchild, Node[root].rchild);     //交换左右孩子结点
}

int strToNum(char c)    //将输入的字符转成-1或者结点编号
{
    if(c == '-')    return -1;  // "-"表示没有孩子结点,记为-1
    else
    {
        notRoot[c - '0'] = true;    //标记根结点不是c
        return c - '0';     //返回结点编号
    }
}

int findRoot()      //寻找根结点编号
{
    for(int i = 0; i < n; i++)
        if(notRoot[i] == false)     //是根结点
            return i;
}


int main()
{
    char lchild, rchild;
    scanf("%d", &n);
    for(int i = 0; i < n; i++)
    {
        getchar();      //吸收回车符
        scanf("%c %c", &lchild, &rchild);   //%c读入格式会将空格读入 所以要%c %c
//        scanf("%*c %c %c", &lchild, &rchild);
        Node[i].lchild = strToNum(lchild);
        Node[i].rchild = strToNum(rchild);
    }
    int root = findRoot();      //获得根结点编号
    postOrder(root);        //后序遍历,反转二叉树
    BFS(root);      //输出层次遍历序列
    num = 0;        //将已输出结点个数置0
    inOrder(root);  //输出中序遍历序列
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值