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

分析

用vector Left和right来记录每个节点的左孩子节点和右孩子节点,利用根节点不是任何节点的子节点来找到根节点root,在从根节点开始将节点依次插入树中,最后就是基本的层序遍历和中序遍历的实现了。

#include<iostream>
#include<vector>
#include<queue>
#include<string.h>
using namespace std;
vector<int> Left;
vector<int> Right;
int flag=1;
class node{
public:
int value;
node* left=NULL;
node* right=NULL;
node(int val):value(val), left(NULL), right(NULL){
}
};
void CreateTree(node* root){
if(Left[root->value]!=-1){
root->left=new node(Left[root->value]);
CreateTree(root->left);
}
if(Right[root->value]!=-1){
root->right=new node(Right[root->value]);
CreateTree(root->right);
}
}
void inorder(node* root){
if(root->left)
inorder(root->left);
flag++==1?cout<<root->value:cout<<" "<<root->value;
if(root->right)
inorder(root->right);
}
int main(){
int N;
cin>>N;
vector<int> record(N,0);
Left.resize(N);
Right.resize(N);
for(int i=0; i<N; i++){
char l, r;
cin>>l>>r;
if(isdigit(l)){
Right[i]=l-'0';
record[Right[i]]=1;
}
else
Right[i]=-1;
if(isdigit(r)){
Left[i]=r-'0';
record[Left[i]]=1;
}
else
Left[i]=-1;
}
int i;
for(i=0; i<N; i++)
if(record[i]==0)
break;
node* root=new node(i);
CreateTree(root);
queue<node*> q;
q.push(root);
while(q.size()!=0){
node* t=q.front();
q.pop();
flag++==1?cout<<t->value:cout<<" "<<t->value;
if(t->left)
q.push(t->left);
if(t->right)
q.push(t->right);
}
cout<<endl;
flag=1;
inorder(root);
return 0;
}

PAT 1102 Invert a Binary Tree的更多相关文章

  1. PAT 1102 Invert a Binary Tree[比较简单]

    1102 Invert a Binary Tree(25 分) The following is from Max Howell @twitter: Google: 90% of our engine ...

  2. PAT甲级——1102 Invert a Binary Tree (层序遍历+中序遍历)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90577042 1102 Invert a Binary Tree ...

  3. 1102 Invert a Binary Tree——PAT甲级真题

    1102 Invert a Binary Tree The following is from Max Howell @twitter: Google: 90% of our engineers us ...

  4. PAT Advanced 1102 Invert a Binary Tree (25) [树的遍历]

    题目 The following is from Max Howell @twitter: Google: 90% of our engineers use the sofware you wrote ...

  5. 1102. Invert a Binary Tree (25)

    The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...

  6. PAT A1102 Invert a Binary Tree (25 分)——静态树,层序遍历,先序遍历,后序遍历

    The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...

  7. PAT (Advanced Level) 1102. Invert a Binary Tree (25)

    简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...

  8. PAT甲题题解-1102. Invert a Binary Tree (25)-(建树,水题)

    就是把输入给的左孩子右孩子互换一下,然后输出层次遍历和中序遍历. #include <iostream> #include <algorithm> #include <c ...

  9. 【PAT甲级】1102 Invert a Binary Tree (25 分)(层次遍历和中序遍历)

    题意: 输入一个正整数N(<=10),接着输入0~N-1每个结点的左右儿子结点,输出这颗二叉树的反转的层次遍历和中序遍历. AAAAAccepted code: #define HAVE_STR ...

随机推荐

  1. 排列(permutation) 用1,2,3,…,9组成3个三位数abc,def和ghi,每个数字恰好使用一次,要 求abc:def:ghi=1:2:3。按照“abc def ghi”的格式输出所有解,每行一个解。

    #include <stdio.h> #include <math.h> // 算法竞赛的目标是编程对任意输入均得到正确的结果. // 请先独立完成,如果有困难可以翻阅本书代码 ...

  2. 杂项:BPM

    ylbtech-杂项:BPM 1.返回顶部 1. BPM,即业务流程管理,是一种以规范化的构造端到端的卓越业务流程为中心,以持续的提高组织业务绩效为目的的系统化方法,常见商业管理教育如EMBA.MBA ...

  3. Gym - 101972B Arabella Collegiate Programming Contest (2018) B. Updating the Tree 树DFS

    题面 题意:T组数据,每次给你1e5个点的树(1为根),每个点有一权值,询问1-n每个节点的子树中, 至少修改几个点的权值(每次都可以任意修改),才能让子树中任意2点的距离==他们权值差的绝对值 无解 ...

  4. 微信小程序图片选择,预览和删除

    这里均用的是小程序原生api 废话不多说直接上栗子: <view class="addImv"> <!--这个是已经选好的图片--> <view wx ...

  5. Dockerfile镜像的制作

    Dockerfile镜像的制作 如果学习Docker,那么制作镜像这一步肯定不能少的,别人给你的是环境,而你自己做的才是你最终需要的东西,接下来就记录一下如何制作一个满足自己的镜像,我们使用docke ...

  6. mysql多表查询 查询排序

    有 ask 问题表  和 answer回答表  回答表中的ask_id和 ask表中的id对应 1.查询 /*查询回答了的 */select a.id,a.title,count(b.ask_id) ...

  7. Criteria 查询

    Criteria.Criterion接口和Expression类组成,他支持在运行时动态生成查询语句. Criteria查询是Hibernate提供的一种查询方式 Hibernate检索方式:  PO ...

  8. Boost Bimap示例

    #include <string> #include <iostream> #include <boost/bimap.hpp> template< clas ...

  9. Android RecyclerView notifyDataSetChanged不起作用

    一般listview设置完data后调用notifyDataSetChanged便可刷新布局界面,然而recycleview调用这个方法却没有任何反应.对于很多不熟悉recycleview的话很容易躺 ...

  10. 新浪云虚拟机ftp链接显示失败问题

    新浪云虚拟机ftp链接显示失败问题 测试是在局域网遇到的 域名解析可以ping有字节回复 账号密码也没有错误,但是链接一直出现    连接失败   拒接连接等问题 解决办法: 其实是局域网内的问题,这 ...