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 ( ...
随机推荐
- Spring+SpringMVC+MyBatis+easyUI整合进阶篇(八)线上Mysql数据库崩溃事故的原因和处理
前文提要 承接前文<一次线上Mysql数据库崩溃事故的记录>,在文章中讲到了一次线上数据库崩溃的事件记录,建议两篇文章结合在一起看,不至于摸不着头脑. 由于时间原因,其中只讲了当时的一些经 ...
- MySQL高可用架构-MHA环境部署记录
一.MHA介绍 MHA(Master High Availability)目前在MySQL高可用方面是一个相对成熟的解决方案,它由日本DeNA公司youshimaton(现就职于Facebook公司) ...
- myBatis外部的resultMap高级应用
resultMap:外部的resultMap的引用,和resultType不能同时使用. <resultMap id="BaseResultMap" type="c ...
- iOS开发线程安全问题
先来看一下代码: - (void)viewDidLoad { [super viewDidLoad]; self.testStr = @"String initial complete&qu ...
- net license tool, EasyLicense !
net license tool, EasyLicense ! 开源 .net license tool, EasyLicense ! 介绍: 过去我常常像是否有一个帮助授权的软件,它可以非常简单 ...
- yii框架通过IP地址来使用gii
这里使用的YII框架的版本是2.0.13 详情请参考官方文档:用Gii生成代码 使用gii的主要步骤 1.生成模型(Model Generator) 2.生成CRUD代码 注意点 1.在生成CURD代 ...
- Undertow的InMemorySessionManager
https://github.com/undertow-io/undertow/blob/master/core/src/main/java/io/undertow/server/session/In ...
- CentOS7 卸载mariadb 安装mysql的过程:
1. 检查安装的mariadb rpm -qa |grep mariadb 得到已经安装的安装包 mariadb-libs-5.5.56-2.el7.x86_64mariadb-devel-5.5.5 ...
- BMC 安装操作系统以及 驱动的处理
1. 在一个郊区的机房有两台机器需要重装系统. 2. 服务器配置了BMC 还不错... 能够节约很多时间. 3. BMC的配置不算复杂, 就是management 网口插上网线,然后配置一下可以使用的 ...
- shell if [[ ]]和[ ]区别 || &&
[]和test 两者是一样的,在命令行里test expr和[ expr ]的效果相同. test的三个基本作用是判断文件.判断字符串.判断整数.支持使用 ”与或非“ 将表达式连接起来. test中可 ...