PAT甲级——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 (≤) 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
先找到根节点,数字r未出现,则r为根节点,因为根节点不是任何节点的子节点
然后静态构造树
再调用平常的层序遍历和中序遍历
所谓的二叉树反转,就是原来先读左子树,再读右子树
现在改为先读右子树,再读左子树
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
struct Node
{
int l, r;
};
int N, root[] = { };
Node tree[];
vector<int>lev, in;
void levelOrde(int t)
{
if (t == -)
return;
queue<int>q;
q.push(t);
while (!q.empty())
{
t = q.front();
q.pop();
lev.push_back(t);
if (tree[t].r != -)//先进右
q.push(tree[t].r);
if (tree[t].l != -)
q.push(tree[t].l);
}
}
void inOrder(int t)
{
if (t == -)
return;
inOrder(tree[t].r);
in.push_back(t);
inOrder(tree[t].l);
}
int main()
{
cin >> N;
char l, r;
for (int i = ; i < N; ++i)
{
cin >> l >> r;
if (l != '-')
{
tree[i].l = l - '';
root[l - ''] = -;//去除为根的可能性
}
else
tree[i].l = -;
if (r != '-')
{
tree[i].r = r - '';
root[r - ''] = -;//去除为根的可能性
}
else
tree[i].r = -;
}
for (int i = ; i < N; ++i)
{
if (root[i] == )
{
r = i;
break;//找到了根节点
}
}
levelOrde(r);
inOrder(r);
for (int i = ; i < N; ++i)
cout << lev[i] << (i == N - ? "" : " ");
cout << endl;
for (int i = ; i < N; ++i)
cout << in[i] << (i == N - ? "" : " ");
return ;
}
PAT甲级——A1102 Invert a Binary Tree的更多相关文章
- PAT甲级——1102 Invert a Binary Tree (层序遍历+中序遍历)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90577042 1102 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 ( ...
- A1102. Invert a Binary Tree
The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...
- 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 ...
- 【PAT甲级】1110 Complete Binary Tree (25分)
题意: 输入一个正整数N(<=20),代表结点个数(0~N-1),接着输入N行每行包括每个结点的左右子结点,'-'表示无该子结点,输出是否是一颗完全二叉树,是的话输出最后一个子结点否则输出根节点 ...
- 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——PAT甲级真题
1102 Invert a Binary Tree The following is from Max Howell @twitter: Google: 90% of our engineers us ...
- PAT甲级:1064 Complete Binary Search Tree (30分)
PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...
- PAT 1102 Invert a Binary Tree[比较简单]
1102 Invert a Binary Tree(25 分) The following is from Max Howell @twitter: Google: 90% of our engine ...
随机推荐
- 【转】Windows(server2008)下使用VisualSVN Server搭建SVN服务器
参考文献 1.Windows下使用VisualSVN Server搭建SVN服务器(百度经验) 挺好就是没有配图已验证可用 2.在Windows Server 2008上部署SVN代码管理器 把第二 ...
- Windows7 打开word2003提示:向程序发送命令时出现错误 解决方案
1.关闭所有打开的Word文档:(包括任务管理器里的进程)2.复制这条命令:%appdata%\microsoft\templates3.开始 → 运行 → 粘贴上面复制的命令 → 确定4.在打开的目 ...
- linux zip,tar压缩文件夹 忽略 .git 文件夾
linux zip 忽略 .git 文件夾 # zip 命令 zip -r bitvolution.zip bitvolution -x *.git* # tar命令压缩文件夹忽略 .git文件夹 t ...
- 微信小程序为什么看不到所有的console.log()的日志信息
记录一个巨傻无比的问题 1.在首页的onLoad()函数里面,加了地理位置的加载,并打印到控制台上,可是今天就是没出现 2.然后纳闷的很久,各种google,发现没有人遇到这个问题 3.再然后,我就看 ...
- day11 grep正则匹配
ps aus | trep nginx # 查看所有正在运行的nginx任务 别名路径: alias test_cmd='ls -l' PATH路径: 临时修改: PATH=$PATH:/usr/lo ...
- thinkphp DEFINE标签
DEFINE标签用于中模板中定义常量,用法如下: 直线电机厂家 <define name="MY_DEFINE_NAME" value="3" /> ...
- HDU5377
题意:给sum,m组询问,每组x,y求\(x^t=y\mod p,p|sum\),p是素数,求最小的t 题解:先处理sum的所有质因子p,求出p的原根rt,\(rt^a=x\mod p,rt^b=y\ ...
- 使用Navicat连接管理远程linux服务器上的mysql数据库
第一步:选择连接,选择mysql 第二步:填写下面弹出框的信息:连接名随便写,主机名或IP地址:写上服务器的ip. 端口不变 用户名不变. 密码:输入服务器数据库的密码12345678. 接着测 ...
- 滑雪 矩阵中的最长上升路径 /// 记忆化DFS || DP oj22919
大致题意: Description 难怪Michael喜欢滑雪,因为滑雪确实很刺激.为了获得加速度,滑雪道必须向下倾斜,而且当滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道在一 ...
- Java核心-03 谈谈final、finally、 finalize有什么不同?
今天,我要问你的是一个经典的 Java 基础题目,谈谈 final.finally. finalize 有什么不同? 典型回答 final 可以用来修饰类.方法.变量,分别有不同地意义,final修饰 ...