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 ...
随机推荐
- Photoshop基础照片美化
自从有了“ps”以后,很多事情变成了可能,你可以出现在任何你想在的地方.而最基本的美化照片的功能,我想是很多同学学习PS的初衷.当你掌握了这门技术,很多人会对你刮目相看!今天小编就和大家分享一下ps的 ...
- Spark RDD API(scala)
1.RDD RDD(Resilient Distributed Dataset弹性分布式数据集)是Spark中抽象的数据结构类型,任何数据在Spark中都被表示为RDD.从编程的角度来看,RDD可以简 ...
- 初学者学习PHP开发应该掌握的几段精华代码
来自:http://hi.baidu.com/dckhello/item/d62b16d8994bf93449e1ddb0 经典循环例子 <HTML><HEAD><TIT ...
- iOS开发系列-iOS布局相关
LayoutSubViews 需要在某个View调整子视图的位置时,可以重写. 以下情况会出发LayoutSubViews方法的调用 init初始化不会触发layoutSubviews,但是是用ini ...
- iOS进阶一OC对象的本质
OC对象的本质 平时编写的Object-C代码,底层实现其实都是C/C++代码. 所以Objective-C的面向对象都是基于C/C++的数据结构实现的,OC对象内部可以容纳不同数据类型的数据,因此可 ...
- 2019-7-29-asp-dotnet-core-从-Frp-获取用户真实-IP-地址
title author date CreateTime categories asp dotnet core 从 Frp 获取用户真实 IP 地址 lindexi 2019-07-29 08:28: ...
- 在egg.js中使用mongodb
1.egg.js官网只推荐了mysqle,要用mongodb得另找资料.通过查找,大家都在用Mongoose连接,于是乎学习. 网站链接:https://www.npmjs.com/package/e ...
- 《parsing techniques》中文翻译和正则引擎解析技术入门
http://parsing-techniques.duguying.net/ (中文版) https://swtch.com/~rsc/regexp/ https://blog.csdn.net/m ...
- Altera: set pin locations using tcl
1, compile the project; 2, store current tcl settings: Project –> Generate Tcl File from Project- ...
- 三模数NTT模板
求两个多项式的卷积对任意数p取模 两个好记的FNT模数: 5*2^25+1 7*2^26+1 原根都为3 //Achen #include<algorithm> #include<i ...