A1102. 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
#include<cstdio>
#include<iostream>
#include<stack>
#include<string.h>
#include<queue>
using namespace std;
typedef struct NODE{
int lchild, rchild;
int data;
}node;
node tree[];
int N, notRoot[] = {,};
void postReverse(int root){
if(root == -)
return;
if(tree[root].lchild != -)
postReverse(tree[root].lchild);
if(tree[root].rchild != -)
postReverse(tree[root].rchild);
swap(tree[root].lchild, tree[root].rchild);
}
void levelOrder(int root, int &cnt){
queue<int> Q;
if(root != -){
Q.push(root);
}
while(Q.empty() == false){
int index = Q.front();
Q.pop();
cnt++;
if(cnt == N){
printf("%d", tree[index].data);
}else{
printf("%d ", tree[index].data);
}
if(tree[index].lchild != -)
Q.push(tree[index].lchild);
if(tree[index].rchild != -)
Q.push(tree[index].rchild);
}
}
void inOrder(int root, int &cnt){
if(root == -)
return;
if(tree[root].lchild != -)
inOrder(tree[root].lchild, cnt);
cnt++;
if(cnt == N)
printf("%d", tree[root].data);
else printf("%d ", tree[root].data);
if(tree[root].rchild != -)
inOrder(tree[root].rchild, cnt);
}
int main(){
char c1, c2;
scanf("%d", &N);
for(int i = ; i < N; i++){
scanf("%*c%c %c", &c1, &c2);
if(c1 == '-'){
tree[i].lchild = -;
}else{
tree[i].lchild = c1 - '';
notRoot[c1 - ''] = ;
}
if(c2 == '-'){
tree[i].rchild = -;
}else{
tree[i].rchild = c2 - '';
notRoot[c2 - ''] = ;
}
tree[i].data = i;
}
int root = ;
for(int i = ; i < N; i++)
if(notRoot[i] == ){
root = i;
break;
}
int cnt = ;
postReverse(root);
levelOrder(root, cnt);
printf("\n");
cnt = ;
inOrder(root, cnt);
cin >> N;
return ;
}
总结:
1、本题要求先对二叉树进行反转(左变成右),再层序和中序输出。由于二叉树的后序遍历是先访问左右子树,再访问根节点,与逆转具有相同的性质。逆转要求在左右子树都已经逆转之后,再将这两颗子树交换位置。因此逆转二叉树可以用后序遍历实现。
2、对于给数字编号、给出每个节点的左右孩子编号的输入数据,最好使用静态二叉树。将节点都保存在一个node数组中,仅仅对数组的下标进行操作。
3、静态二叉树寻找root:使用数组notRoot坐标记,在读入节点时,如果将其孩子节点标记为notRoot。输入完毕后遍历数组寻找root节点。
4、由于%c会将上一行的 \n 读入,所以每行之前要吸收 \n, 两个字符之间还要匹配空格。可以 scanf("%*c%c %c", &c1, &c2); 其中%*c会读入一个字符,但被忽略,接收参数的是后两个。
A1102. Invert a Binary Tree的更多相关文章
- 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甲级——A1102 Invert a Binary Tree
The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...
- PAT_A1102#Invert a Binary Tree
Source: PAT A1102 Invert a Binary Tree (25 分) Description: The following is from Max Howell @twitter ...
- 1102. Invert a Binary Tree (25)
The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...
- Invert a binary tree 翻转一棵二叉树
Invert a binary tree 翻转一棵二叉树 假设有如下一棵二叉树: 4 / \ 2 7 / \ / \ 1 3 6 9翻转后: 4 / \ 7 ...
- PAT1102: Invert a Binary Tree
1102. Invert a Binary Tree (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- 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 ...
- PAT 1102 Invert a Binary Tree
The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...
随机推荐
- js类型----你所不知道的JavaScript系列(5)
ECMAScirpt 变量有两种不同的数据类型:基本类型,引用类型.也有其他的叫法,比如原始类型和对象类型等. 1.内置类型 JavaScript 有七种内置类型: • 空值(null) • 未定义( ...
- [T-ARA N4/二段横踢][Can We Love]
歌词来源:http://music.163.com/#/song?id=26310867 Can We Love Can We Love [Can We Love Can We Love] Can W ...
- Redis常用操作-------List(列表)
1.BLPOP key [key ...] timeout BLPOP 是列表的阻塞式(blocking)弹出原语. 它是 LPOP 命令的阻塞版本,当给定列表内没有任何元素可供弹出的时候,连接将被 ...
- Linux内核分析作业八
进程的切换和系统的一般执行过程 贾瑗 原创作品转载请注明出处 <Linux内核分析>MOOC课程 http://mooc.study.163.com/course/USTC-1000029 ...
- beta(3/7)
团队信息 队名:爸爸饿了 组长博客:here 作业博客:here 组员情况 组员1(组长):王彬 过去两天完成了哪些任务 协助后端完成历史记录接口.美食排行榜接口 完成食堂平面图的绘制 确定web端业 ...
- <面向对象程序设计>课程作业一
Github链接 在看完这次的作业要求后我整个人是混乱的,因为作业要求把不同的函数放在一个main函数中:我们之前也是进行了函数分离,但是是放在了不同的文件中.如果要改的话相当于重写(而且这两种形式其 ...
- MySql修改root密码以及允许外网访问
1.修改root密码 用SET PASSWORD命令 mysql -u root mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('n ...
- node.js依赖express解析post请求四种数据格式()
分别是这四种: www-form-urlencoded, form-data, application/json, text/xml www-form-urlencoded 这是http的post请求 ...
- canvas高斯模糊算法
对于模糊图片这个效果的实现,其实css3中的filter属性也能够实现,但是这个属性的兼容性不是很好,所以我们通常不用这种方法实现,而使用canvas配合JS实现. <span style=&q ...
- Fantacy团队周一站立会议
词频分析模型 1.首先这次站会是周一开的,但是由于我个人的疏忽,没有落实到博客上,请见谅,连累了组长. 2.会议时间:2016年3月28日12:00~12:30. 持续时长:30分钟 会议参加成员:组 ...