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. 又发现2个高人写的Delphi图文并茂的消息研究

    http://ymg97526.blog.163.com/blog/static/173658160201139101120862/http://ymg97526.blog.163.com/blog/ ...

  2. E - 吃糖

    题目描述: 某人买了n兜糖果,第i兜有Ai块糖.此人把所有这些糖果用一个数字标记起来:他这样标记这些糖,第一袋糖用用数字1到A1,第二袋糖用数字A1+1到A1+A2,如此类推.如果还没明白看样例可以更 ...

  3. lightbox2

    http://lokeshdhakar.com/projects/lightbox2/ 简单的demo <html> <head id="head"> &l ...

  4. @注解与普通web.xml的关系

    @WebServlet(name = "SimpleServlet" ,urlPatterns = {"/simple"}) public class Simp ...

  5. 0619-dedeCMS数据表

    CMS的层级从前台分主要分为首页--栏目页--内容页,从后台分主要是四张表之间的关系: 1.模型表--dede_channeltype(顶级) 2.栏目表--dede_arctype 3.数据表:分为 ...

  6. 【Kafka】《Kafka权威指南》——从Kafka读取数据

    应用程序使用 KafkaConsumer向 Kafka 订阅主题,并从订阅的主题上接收消息 . 从 Kafka 读取数据不同于从其他悄息系统读取数据,它涉及一些独特的概念和想法.如果不先理解 这些概念 ...

  7. 一款超好用的第三方评论插件--Gitalk

    一,使用Gitalk的背景: 1.最近在做一个基于Java的个人博客系统,已经基本完工了,突然发现怎么没有评论的操作,如果再从头开始从数据库开始写的话,花费的代价有点大,于是乎我就在网上寻找一款适合我 ...

  8. Fib(兔子问题)python实现多种方法

    # 斐波那契数列是学计算机入门最经典的一道题目 # 斐波那契数列(Fibonacci sequence),又称黄金分割数列.因数学家列昂纳多·斐波那契(Leonardoda Fibonacci) # ...

  9. flask中内置的session

    Flask中的Session非常的奇怪,他会将你的SessionID存放在客户端的Cookie中,使用起来也非常的奇怪 1. Flask 中 session 是需要 secret_key 的 from ...

  10. 元素属性的添加删除(原生js)

    添加属性 odiv.setAttribute("title","hello div!"); odiv.setAttribute("class" ...