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. HDU—4046 Panda (线段树)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=4046   题意:给出一个字符串,统计这个字符串任意区间中"wbw"出现的次数. 规定两 ...

  2. eclipse导出说明文档

    选中项目--右键--Export--Java--Javadoc—Finish 1.为程序添加文档注释 2.选中项目--右键Export--Java--Javadoc--next, 3.next--在V ...

  3. [JZOJ3339]【NOI2013模拟】wyl8899和法法塔的游戏

    题目 题目大意 给你一个数列,每次给出\(r,a,b\),你要找到\(l\in [a,b]\)使得\([l,r-1]\)的异或和最小, 并且要修改\(r\)位置的数. 思考历程 当我看到这题的时候,已 ...

  4. 阿里云宣布进入 Serverless 容器时代,推出弹性容器实例服务 ECI

    摘要: 阿里云宣布弹性容器实例 ECI(Elastic Container Instance)正式商业化. 为了应对业务高峰,打算提前多久执行ECS扩展?买了ECS虚拟机,容器规格不能完美装箱怎么办? ...

  5. ROS 自定义消息类型方法

    流程 1.在package中新建文件夹名为msg 2.在msg文件夹中创建消息(此处以my_msg.msg)为例,注意的是要以msg为后缀名 内容举例如下: int32 data1 float64 d ...

  6. Eclipse中如何使用Hibernate

    首先创建一个java web项目,其目录如下: (1)创建文件夹hibernate4(用于存放下载的hibernate工具包lib/required文件夹下所有的jar包),jar包目录如下: (2) ...

  7. 网络安全系列 之 SQL注入学习总结

    目录 1. sql注入概述 2. sql注入测试工具 3. sql注入防御方法 3.1 问题来源 3.2 防御方法 4. SQL注入防御举例 4.1 使用JDBC时,SQL语句进行了拼接 4.2 使用 ...

  8. 《DSP using MATLAB》Problem 8.40

    代码: function [wpLP, wsLP, alpha] = bs2lpfre(wpbs, wsbs) % Band-edge frequency conversion from bandst ...

  9. ssm 框架整合 代码初步 maven配置

    pom.xml 配置<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <de ...

  10. 浅谈web应用的高可用

    1.熟悉几个组件 1.1.apache     —— 它是Apache软件基金会的一个开放源代码的跨平台的网页服务器,属于老牌的web服务器了,支持基于Ip或者域名的虚拟主机,支持代理服务器,支持安全 ...