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 ( ...
随机推荐
- 记录网件r6220路由器登录配置
1.设置本地连接为自动获取ip和DNS地址 2.使用网线连接电脑和路由器的LAN口 3.http://routerlogin.net/BRS_index.htm 4.用户名和密码: admin pas ...
- Tomcat利用MSM实现Session共享方案解说
Session共享有多种解决方法,常用的有四种:1)客户端Cookie保存2)服务器间Session同步3)使用集群管理Session(如MSM) 4)把Session持久化到数据库 针对上面Sess ...
- LVS+Keepalived 高可用环境部署记录(主主和主从模式)
之前的文章介绍了LVS负载均衡-基础知识梳理, 下面记录下LVS+Keepalived高可用环境部署梳理(主主和主从模式)的操作流程: 一.LVS+Keepalived主从热备的高可用环境部署 1)环 ...
- 跟踪分析Linux内核的启动过程
潘俊洋 原创作品转载请注明出处<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.准备 搭建环境 1 2 ...
- 在Java中执行Tomcat中startup.bat
问题:更改数据库时,需要重启Tomcat服务器,才能把更改后的数据加载到项目中.于是想每次更改数据库时,都调用Java方法,重启Tomcat 代码: Process process = Runtime ...
- is interest important?
学习是不是一定要看兴趣呢?高中时觉得只要肯学即使不喜欢又能如何,大学之后被深深打脸,面对一周那么多的实习和报告,我悄悄告诉自己不是这块料 有一些事情我就是学不会.我却很容易相信一个人. 因此,无论我如 ...
- JavaScript 作用域链与闭包
作用域链 在某个作用域访问某个变量或者函数时,会首先在自己的局部环境作用域中搜寻变量或者函数,如果本地局部环境作用域中有该变量或者函数,则就直接使用找到的这个变量值或者函数:如果本地局部环境作用域中没 ...
- Jquery 组 radio控与tr变色
<!DOCTYPE html><html lang="zh-cn"><head> <meta charset="utf-8&qu ...
- String()与toString的区别
1..toString()可以将所有的的数据都转换为字符串,但是要排除null 和 undefined 代码示例: var a = null.toString()--报错 var b = underf ...
- 洛谷 P3657 [USACO17FEB]Why Did the Cow Cross the Road II P
题面 大意:让你把两个n的排列做匹配,连线不想交,而且匹配的数字的差<=4,求最大匹配数 sol:(参考了kczno1的题解)对于第一个排列从左往右枚举,用树状数组维护到达另一个序列第i个数字的 ...