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

题目大意:反转一棵二叉树,给出原二叉树的每个结点的左右孩子,输出它的层序和中序遍历~

分析:1. 反转二叉树就是存储的时候所有左右结点都交换。
2. 二叉树使用{id, l, r, index, level}存储每个结点的id, 左右结点,下标值,和当前层数~
3. 根结点是所有左右结点中没有出现的那个结点~
4. 已知根结点,用递归的方法可以把中序遍历的结果push_back到数组v1里面,直接输出就是中序,排序输出就是层序(排序方式,层数小的排前面,相同层数时,index大的排前面)

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct node {int id, l, r, index, level;
} a[100];
vector<node> v1;
void dfs(int root, int index, int level) {if (a[root].r != -1) dfs(a[root].r, index * 2 + 2, level + 1);v1.push_back({root, 0, 0, index, level});if (a[root].l != -1) dfs(a[root].l, index * 2 + 1, level + 1);
}
bool cmp(node a, node b) {if (a.level != b.level) return a.level < b.level;return a.index > b.index;
}
int main() {int n, have[100] = {0}, root = 0;cin >> n;for (int i = 0; i < n; i++) {a[i].id = i;string l, r;cin >> l >> r;if (l != "-") {a[i].l = stoi(l);have[stoi(l)] = 1;} else {a[i].l = -1;}if (r != "-") {a[i].r = stoi(r);have[stoi(r)] = 1;} else {a[i].r = -1;}}while (have[root] == 1) root++;dfs(root, 0, 0);vector<node> v2(v1);sort(v2.begin(), v2.end(), cmp);for (int i = 0; i < v2.size(); i++) {if (i != 0) cout << " ";cout << v2[i].id;}cout << endl;for (int i = 0; i < v1.size(); i++) {if (i != 0) cout << " ";cout << v1[i].id;}return 0;
}

1102. Invert a Binary Tree (25)-PAT甲级真题相关推荐

  1. 1102 Invert a Binary Tree (25 分)

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

  2. 1020. Tree Traversals (25) PAT甲级真题

    之前我看了这道题,实在是看不懂网上的解题答案,他们的具体思路基本上就是通过后续遍历和中序遍历,直接推出层次遍历. 我苦思冥想了半天,是在没看懂这种思路,于是想了一个笨点的但是也比较好理解的思路,通过后 ...

  3. 1078. Hashing (25)-PAT甲级真题

    1078. Hashing (25) The task of this problem is simple: insert a sequence of distinct positive intege ...

  4. 1040. Longest Symmetric String (25)-PAT甲级真题

    Given a string, you are supposed to output the length of the longest symmetric sub-string. For examp ...

  5. 1006. Sign In and Sign Out (25)-PAT甲级真题

    At the beginning of every day, the first person who signs in the computer room will unlock the door, ...

  6. 1121. Damn Single (25)-PAT甲级真题

    "Damn Single (单身狗)" is the Chinese nickname for someone who is being single. You are suppo ...

  7. 1090. Highest Price in Supply Chain (25)-PAT甲级真题

    A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...

  8. 1106. Lowest Price in Supply Chain (25)-PAT甲级真题(dfs,bfs,树的遍历)

    A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...

  9. 1114. Family Property (25)-PAT甲级真题(并查集)

    This time, you are supposed to help us collect the data for family-owned property. Given each person ...

最新文章

  1. 01 python爬虫
  2. oracle tns连接拒绝,TNS-12564: TNS: 拒绝连接(new)
  3. python读取输入流_python – 将一个正在运行的程序的输出流传输到其他正在运行的程序的输入流...
  4. Mac MacBook Pro的移动硬盘方案
  5. 还不起9亿?有人建议为范冰冰发行一款私募ABS产品融资!
  6. sql在insert的同时把某个字段返回来_项目实践:后端接口统一规范的同时,如何优雅得扩展规范?...
  7. 俄罗斯政府称“主权网络”测试成功
  8. kubectl apply -f weave.yaml之后dns没有启动起来 weave-net CrashLoopBackOff
  9. 【MyBatis】动态SQL中的参数判空
  10. linux有名管道 复用,Linux进程间通信(九)---综合实验之有名管道通信实验
  11. App 常用图标尺寸规范汇总
  12. 反相器有时候为了强调低电平有效,将反相器的图形符号中的小圆圈画在输入端,如数电中的画法。有时候小圆圈在前面只表示低电平有效,没有反相的意思,例如与非门组成的RS触发器
  13. 插入排序和迭代归并排序以及复杂度分析
  14. Access Key / Secret key 密钥安全原理架构
  15. 解决Mac苹果电脑没有声音,喇叭会显示为灰色禁用状态
  16. C++蓝桥杯贪心算法
  17. STM32驱动开发(二)--USB Device RNDIS虚拟网卡(usb hound抓包完整数据流分析)
  18. QT6.2关于坐标显示
  19. web.firewall.RequestRejectedException: The request was rejected because the URL contained a potentia
  20. LVGL 字体转换与支持

热门文章

  1. NFC Enable 过程分析(三)
  2. 前端算法-基本排序算法比较
  3. 通过EmbeddedServletContainerCustomizer接口调优Tomcat
  4. GHOSTXP_SP3
  5. linux 编写shell管理脚本01。2
  6. 对于基于模板的前端框架的补充
  7. 开源云原生平台 Apache Kafka暴露多家大企业的敏感数据
  8. 2021北京网络安全大会议程及直播观看指南
  9. 我发现了 Microsoft Azure 中的两个漏洞
  10. 奇安信代码安全实验室五人入选“2020微软 MSRC 最具价值安全研究者”榜单