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的更多相关文章

  1. PAT甲级——1102 Invert a Binary Tree (层序遍历+中序遍历)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90577042 1102 Invert a Binary Tree ...

  2. PAT A1102 Invert a Binary Tree (25 分)——静态树,层序遍历,先序遍历,后序遍历

    The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...

  3. A1102. Invert a Binary Tree

    The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...

  4. 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 ...

  5. 【PAT甲级】1110 Complete Binary Tree (25分)

    题意: 输入一个正整数N(<=20),代表结点个数(0~N-1),接着输入N行每行包括每个结点的左右子结点,'-'表示无该子结点,输出是否是一颗完全二叉树,是的话输出最后一个子结点否则输出根节点 ...

  6. PAT_A1102#Invert a Binary Tree

    Source: PAT A1102 Invert a Binary Tree (25 分) Description: The following is from Max Howell @twitter ...

  7. 1102 Invert a Binary Tree——PAT甲级真题

    1102 Invert a Binary Tree The following is from Max Howell @twitter: Google: 90% of our engineers us ...

  8. PAT甲级:1064 Complete Binary Search Tree (30分)

    PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...

  9. PAT 1102 Invert a Binary Tree[比较简单]

    1102 Invert a Binary Tree(25 分) The following is from Max Howell @twitter: Google: 90% of our engine ...

随机推荐

  1. 2018-8-10-VisualStduio-打断点调试和不打断点调试有什么区别

    title author date CreateTime categories VisualStduio 打断点调试和不打断点调试有什么区别 lindexi 2018-08-10 19:16:52 + ...

  2. 数据库MySQL--条件查询/排序查询

    一.条件查询 条件查询:满足条件的字段被筛选出来 语法:select 查询列表字段 from 表名 where 筛选条件: 条件查询的条件分类: 1.按条件表达式筛选:条件运算符:>, < ...

  3. scala中的闭包简单使用

    object Closure { /** * scala中的闭包 * 函数在变量不处于其有效作用域内,还能够对变量进行访问 * * @param args */ def main(args: Arra ...

  4. Flink SQL 系列 | 5 个 TableEnvironment 我该用哪个?

    本文为 Flink SQL 系列文章的第二篇,前面对 Flink 1.9 Table 新架构及 Planner 的使用进行了详细说明,本文详细讲解 5 个 TableEnvironment 及其适用场 ...

  5. day29 面向对象入门

    Python之路,Day17 = Python基础17-面向对象入门 创建类和对象 面向对象编程是一种编程方式,此编程方式的落地需要使用 “类” 和 “对象” 来实现,所以,面向对象编程其实就是对 “ ...

  6. Exception from HRESULT:

    在MFC工程中,在类向导的时候,偶尔会遇到 "Exception from HRESULT:" 的问题,问题的原因可能是移动工程之类的操作破坏了工程的某些文件或者更改了某些路径的映 ...

  7. csp-s模拟测试90

    csp-s模拟测试90 考场发明$Spfa$祭. $T1$按照题意模拟,然后我就发现我死了.一气之下删掉了$priority$,拍了几下发现贼jb快而且还是对的就开心地交了.$T2$的差分状态定义很棒 ...

  8. (转)I帧,P帧,B帧 .

    转:http://blog.csdn.net/abcjennifer/article/details/6577934 视频压缩中,每帧代表一幅静止的图像.而在实际压缩时,会采取各种算法减少数据的容量, ...

  9. 创建自定义ssl证书用于https

    这里,不探究证书原理.我们要完成的任务是,自己充当CA,然后签出证书供服务器使用. 本次教程是在windows实现,实验之前,确认自己的电脑中有openssl程序.如果没有,博主帮你准备了一个:htt ...

  10. 通过aapt查看apk包名和第一个启动的activity

    步骤: ps:aapt是sdk 自带的一个工具,在sdk\builds-tools目录下: 1. cmd启动控制台, 默认是c盘,输入“d:” 即可转到D盘目录 2. 到D盘后 输入cd 子文件目录转 ...