PAT甲级刷题记录——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

思路

这题的思路肯定是建立一棵静态二叉树(因为不像之前的题目一样直接给序列,这题是给出父亲孩子的关系,用指针的话比较麻烦),然后静态二叉树写的时候不要写错了(我一开始写成了完全二叉树的静态……因为第一反应就是左右孩子是当前结点编号的两倍或者两倍加1,但是普通的静态树是不满足这个关系的),其实和普通的指针二叉树是一样的,只不过指针域变成了int型(也就是数组下标)。

还有一个是获得根结点的问题,我在创建完树之后居然在如何寻找根结点的问题上卡了好久(我怎么可以这么笨)……其实很简单,因为题目给出了一组一组的父子关系,因此,当某一个点没有父亲结点的话,那它就是根结点了(比如题目给出的1 0 2 7 5 4 6都是某个结点的孩子,那么它们肯定不是根结点(因为有父亲结点),于是,没出现过的结点3就一定是根结点了)。

找到根结点之后就非常好办了,直接放到层序和中序的函数里就行啦~

(不过要记得层序和中序的函数要改一下,因为现在是静态树)

代码

#include<cstdio>
#include<stdlib.h>
#include<algorithm>
#include<queue>
#include<string>
#include<string.h>
#include<iostream>
using namespace std;
const int maxn = 1024;//最大下标地址是2^10-1
struct node{
    int data;
    int lchild;
    int rchild;
}Node[maxn];
bool isRoot[maxn];
void inorder(int root, int &cnt){
    if(root==-1) return;
    inorder(Node[root].lchild, cnt);
    if(cnt==0){
        cout<<Node[root].data;
        cnt++;
    }
    else{
        cout<<" "<<Node[root].data;
        cnt++;
    }
    inorder(Node[root].rchild, cnt);
}
void levelorder(int root, int &cnt){
    queue<int> q;
    q.push(root);
    while(!q.empty()){
        int top = q.front();
        q.pop();
        if(cnt==0){
            cout<<Node[top].data;
            cnt++;
        }
        else{
            cout<<" "<<Node[top].data;
            cnt++;
        }
        if(Node[top].lchild!=-1) q.push(Node[top].lchild);
        if(Node[top].rchild!=-1) q.push(Node[top].rchild);
    }
}
int main(){
    int N;
    cin>>N;
    cin.ignore();
    int maxi = 0;
    for(int i=0;i<N;i++){
        isRoot[i] = true;//初始化所有结点都是根结点(初始化为true时一定要循环遍历)
    }
    for(int i=0;i<N;i++){
        string temp;
        getline(cin, temp);
        Node[i].data = i;//数据域是自己的结点编号
        if(temp[0]!='-'){
            Node[i].rchild = temp[0]-'0';
            isRoot[temp[0]-'0'] = false;//此时temp[0]有父亲,因此不是根结点
        }
        else Node[i].rchild = -1;
        if(temp[2]!='-'){
            Node[i].lchild = temp[2]-'0';
            isRoot[temp[2]-'0'] = false;
        }
        else Node[i].lchild = -1;
    }
    int cnt = 0;
    int root = 0;
    for(int i=0;i<N;i++){
        if(isRoot[i]==true){
            root = i;
            break;
        }
    }
    levelorder(root, cnt);
    cout<<'\n';
    cnt = 0;
    inorder(root, cnt);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值