PAT 1102 Invert a Binary Tree

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. ——————————-Max Howell @twitter

哈哈哈这句话实在是太有趣了。
既然是为了输出遍历序列的话,那就没有必要去重新构建一颗新的树。需要注意的是,由于scanf的%c格式是可以读入换行符的,因此需要在每行输入前把还是那个一行的换行符接受了。getchar是可以的,另外scanf(“%*c”)也是可以的。

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <cstdio>
#include <map>
#include <math.h>
#include <string.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
using namespace std;

const int maxn = 11;
int n;
int num = 0;
int pre[maxn],in[maxn],post[maxn];
int flag[maxn] = {0};

int node[maxn][2];

void invert_level(int root)
{
    queue<int> Q;
    Q.push(root);

    while(!Q.empty())
    {
        int tmp;
        tmp = Q.front();
        Q.pop();
        printf("%d",tmp);
        num++;
        if(num < n)
            printf(" ");
        else
            printf("\n");

        if(node[tmp][1]!=-1)
            Q.push(node[tmp][1]);
        if(node[tmp][0]!=-1)
            Q.push(node[tmp][0]);
    }
}

void invert_in(int root)
{
    if(root == -1)
        return;
    invert_in(node[root][1]);
    num++;
    printf("%d",root);
    if(num<n)
        printf(" ");
    invert_in(node[root][0]);
}

int main()
{
    int i;
    char j,k;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%*c%c %c", &j,&k);

        if(j!='-')
        {
            node[i][0]= j-'0';
            flag[j-'0'] = 1;
        }
        else
            node[i][0] = -1;

        if(k!='-')
        {
            node[i][1] = k-'0';
            flag[k-'0'] = 1;
        }
        else
            node[i][1] = -1;
    }
    int root;

    for(i=0;i<n;i++)
        if(flag[i]==0)
        {
            root = i;
            break;
        }

    invert_level(root);
    num = 0;
    invert_in(root);

    return 0;
}

然后是算法笔记的invert版本。核心就是用后序遍历去翻转二叉树,因为后序遍历会先访问左边的节点,再访问右边的节点。访问完了以后,再访问根节点。所以这样就可以在访问根节点的时候把左右儿子互换一下。

postOrder(Node[root].lchild);
postOrder(Node[root].rchild);
swap(Node[root].lchild, Node[root].rchild);

这是反转了以后我再去进行其他遍历的例子。总的代码如下:

#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;

const int maxn = 11;
struct node{
    int lchild,rchild;
}Node[maxn];

bool notRoot[maxn] = {false};
int num,n = 0;

void print(int id)
{
    printf("%d", 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 now = q.front();
        q.pop();
        print(now);
        if(Node[now].lchild!=-1)
            q.push(Node[now].lchild);
        if(Node[now].rchild!=-1)
            q.push(Node[now].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)
{
    if(c == '-')
        return -1;
    else
    {
        notRoot[c - '0'] = true;
        return c - '0';
    }

}

int findRoot()
{
    for(int i = 0;i<n;i++)
    {
        if(notRoot[i] == false)
            return i;
    }
    return -1;
}

int main()
{
    char lchild,rchild;
    scanf("%d", &n);
    for(int i=0;i<n;i++)
    {
        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;
    inOrder(root);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值