PAT 1102 Invert a Binary Tree
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的更多相关文章
- PAT 1102 Invert a Binary Tree[比较简单]
1102 Invert a Binary Tree(25 分) The following is from Max Howell @twitter: Google: 90% of our engine ...
- PAT甲级——1102 Invert a Binary Tree (层序遍历+中序遍历)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90577042 1102 Invert a Binary Tree ...
- 1102 Invert a Binary Tree——PAT甲级真题
1102 Invert a Binary Tree The following is from Max Howell @twitter: Google: 90% of our engineers us ...
- 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 ...
- 1102. Invert a Binary Tree (25)
The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...
- PAT A1102 Invert a Binary Tree (25 分)——静态树,层序遍历,先序遍历,后序遍历
The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...
- PAT (Advanced Level) 1102. Invert a Binary Tree (25)
简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...
- PAT甲题题解-1102. Invert a Binary Tree (25)-(建树,水题)
就是把输入给的左孩子右孩子互换一下,然后输出层次遍历和中序遍历. #include <iostream> #include <algorithm> #include <c ...
- 【PAT甲级】1102 Invert a Binary Tree (25 分)(层次遍历和中序遍历)
题意: 输入一个正整数N(<=10),接着输入0~N-1每个结点的左右儿子结点,输出这颗二叉树的反转的层次遍历和中序遍历. AAAAAccepted code: #define HAVE_STR ...
随机推荐
- ASP.NET 4 and Visual Studio 2010
https://msdn.microsoft.com/en-us/library/ee532866.aspx The topics in this section provide informatio ...
- go语言笔记——指针,和C用法以及本质一样,但不支持指针的+-运算!
4.4.2 值类型和引用类型 所有像 int.float.bool 和 string 这些基本类型都属于值类型,使用这些类型的变量直接指向存在内存中的值. Go 语言的取地址符是 &,放到一个 ...
- 【POJ 3190】 Stall Reservations
[题目链接] http://poj.org/problem?id=3190 [算法] 将这些牛按开始吃草的时间排序 维护一个数组S,Si表示畜栏i进去的最后一头牛结束吃草的时间,对于每头牛,找任意一个 ...
- PL/SQL程序控制结构及在PL/SQL中更改数据和管理事务
1.条件控制 A. IF条件分支语法: if (条件1) then 语句; elsif (条件2) then 语句; elsif (条件3) then 语句; else 语句; end if; B . ...
- ORACLE数据删除数据删除的解决办法
今天主要以oracle数据库为例,介绍关于表中数据删除的解决办法.(不考虑全库备份和利用归档日志)删除表中数据有三种方法:·delete(删除一条记录)·drop或truncate删除表格中数据 1. ...
- ubuntu下设置共享目录
在使用VirtualBox和相关的客户机系统比如XPMac等需要用到一些相关功能共享剪贴板等等这时候需要安装VirtualBox中的一个工具叫做Guest Additions中文叫法不一增强工具包功能 ...
- Windows7安装SQLServer 2008图解
Windows7安装SQL Server 2008图解 这几天因为需要,一直想安装SQL Server 2008来作为Web后台的数据库进行些实验,但总是没有时间,今天终于有时间了,便安 ...
- Counterfeit Dollar
http://poj.org/problem?id=1013 #include<stdio.h> #include<string.h> #include<math.h&g ...
- P4280 [AHOI2008]逆序对
传送门 有一个不会证明的贪心:从左到右考虑每一个位置,然后在每一个位置都贪心选取能让该位置构成的逆序对最少的数.判断逆序对的话只要记一下前缀小于等于某数的总数和后缀小于等于某数的总数就行了 //min ...
- Gym - 100920H 2010-2011 OpenCup IX Onsite, II Yandex Summer School H.Squares 暴力
题面 题意:有10w个点,问你选4个点,能组成平行于坐标轴的正方形有多少个 题解:不知道正解,我的做法就是暴力的基础上优化一点,每次按x排好序,每次枚举的2个点都是x相同的 这样算是个优化?但并不能过 ...