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 中配置log4j日志功能
一,添加log4j依赖包 可从官网上下载该依赖包log4j-x.x.xx.jar,下载后 build path,添加依赖包 二,创建 log4j.properties 配置文件 log4j.prope ...
- SpringBoot日记——分布式篇
思考:什么是分布式?什么是微服务? 一些概念:RPC-远程过程调用,某台机器想要调用另一台机器所需要的一种服务,及分布式的服务框架,比如dubbo或者SpringCloud. 铺天盖地的分布式互联网系 ...
- 常见 Bash 内置变量介绍
目录 $0$1, $2 等等$#$* 与 "$*"$@ 与 "$@"$!$_$$$PPID$?$BASH$BASH_VERSION$EUID 与 $UID$GR ...
- 深入理解USB流量数据包的抓取与分析
0x01 问题提出 在一次演练中,我们通过wireshark抓取了一个如下的数据包,我们如何对其进行分析? 0x02 问题分析 流量包是如何捕获的? 首先我们从上面的数据包分析可以知道,这是个USB的 ...
- 个人阅读作业WEEK7 (软件工程的瀑布, 大泥球, 教堂,集市,和银弹)
一 . 关于银弹 (Silver Bullet) 银弹,被引申为解决问题的有效办法.IBM大型机之父福瑞德·布鲁克斯在1986年的论文<没有银弹>中表达了他的观点:软件工程中不存在银弹—— ...
- PAT 1061 判断题
https://pintia.cn/problem-sets/994805260223102976/problems/994805268817231872 判断题的评判很简单,本题就要求你写个简单的程 ...
- Hadoop技术里面有BSP模型、MPI模型
MPI模型,各种编程语言的库挺多. BSP模型,刚才知道.
- [wiki]陶德曼调停
陶德曼调停[编辑] 维基百科,自由的百科全书 凯申物流差点和谈 目录 1背景 2调停经过 3评价 4参见 背景[编辑] 主条目:中德合作 (1911年-1941年) 1936年11月25日,德国与日本 ...
- layer 中 的type和 content
type - 基本层类型 类型:Number,默认:0 layer提供了5种层类型.可传入的值有:0(信息框,默认)1(页面层)2(iframe层)3(加载层)4(tips层). 若你采用layer. ...
- Test Scenarios for a window
1 check if default window size is correct2 check if child window size is correct3 check if there is ...