1102 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 (<=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

题目大意:二叉树的每个结点按照从0~N - 1编号,先输入一个整数N,然后依次输入当前结点的左孩子编号和右孩子编号,如果有孩子不存在用'-'来代替。然后反转这棵二叉树,输出反转过后的二叉树的层次遍历,和中序遍历。

这道题因为最简单的字母输入卡了好久也是没谁了~~~。

大致思路:首先按照题目的要求进行读入构建二叉树,构建玩二叉树之后我们要找到二叉树的根节点。根据二叉树的性质可知,二叉树的根节点没有前驱,所以我们在读入的时候把有前驱的结点都标记一下然后找没有前驱的结点。然后递归反转二叉树,输出二叉树的层次遍历和中序遍历。

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int N = 30;
  4. struct TreeNode {
  5. int id;
  6. int left, right;
  7. }root[N];
  8. int n;
  9. bool vis[N];
  10. vector<int> levelorder, inorder;
  11. void newNode(int x) {
  12. root[x].id = x;
  13. root[x].left = root[x].right = -1;
  14. }
  15. //递归的开始反转二叉树
  16. void invert(int x) {
  17. if (x == -1) return ;
  18. swap(root[x].left, root[x].right);
  19. invert(root[x].left);
  20. invert(root[x].right);
  21. }
  22. void BFS(int x) {
  23. queue<int> q;
  24. q.push(root[x].id);
  25. while(!q.empty()) {
  26. int t = q.front(); q.pop();
  27. levelorder.push_back(t);
  28. if (root[t].left != -1) q.push(root[t].left);
  29. if (root[t].right != -1) q.push(root[t].right);
  30. }
  31. }
  32. void invist(int x) {
  33. if (x == -1) return ;
  34. invisit(root[x].left);
  35. inorder.push_back(x);
  36. invist(root[x].right);
  37. }
  38. int main() {
  39. cin >> n;
  40. memset(vis, 0, sizeof(vis));
  41. for (int i = 0; i < n; i++) newNode(i);
  42. char ch1, ch2;
  43. for (int i = 0; i < n; i++) {
  44. scanf("%c %c", &ch1, &ch2);
  45. if (ch1 != '-') {
  46. root[i].left = ch1 - '0';
  47. vis[ch1 - '0'] = true;
  48. }
  49. if (ch2 != '-') {
  50. root[i].right = ch2 - '0';
  51. vis[ch2 - '0'] = true; //如果有指针指向这个点,则这个点不是根节点
  52. }
  53. }
  54. int root_index;
  55. for (int i = 0; i < n; i++) {
  56. if (!vis[i]) {
  57. root_index = i; //没有结点指向这个点,则这个点为根节点
  58. break;
  59. }
  60. }
  61. BFS(root_index); invist(root_index);
  62. for (int i = 0; i < levelorder.size(); i++) {
  63. cout << levelorder[i];
  64. if (i != levelorder.size() - 1) cout << " ";
  65. else cout << endl;
  66. }
  67. for (int i = 0; i < inorder.size(); i++) {
  68. cout << inorder[i];
  69. if (i != inorder.size() - 1) cout << " ";
  70. else cout << endl;
  71. }
  72. return 0;
  73. }

1102 Invert a Binary Tree——PAT甲级真题的更多相关文章

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

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

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

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

  3. PAT 甲级真题题解(63-120)

    2019/4/3 1063 Set Similarity n个序列分别先放进集合里去重.在询问的时候,遍历A集合中每个数,判断下该数在B集合中是否存在,统计存在个数(分子),分母就是两个集合大小减去分 ...

  4. PAT 1102 Invert a Binary Tree

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

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

  6. 1102. Invert a Binary Tree (25)

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

  7. 1086 Tree Traversals Again——PAT甲级真题

    1086 Tree Traversals Again An inorder binary tree traversal can be implemented in a non-recursive wa ...

  8. 1020 Tree Traversals——PAT甲级真题

    1020 Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. Give ...

  9. PAT 甲级真题题解(1-62)

    准备每天刷两题PAT真题.(一句话题解) 1001 A+B Format  模拟输出,注意格式 #include <cstdio> #include <cstring> #in ...

随机推荐

  1. DEDECMS:解决无法上传图片(在后台插入图片时提示类型不允许)

    在include/uploadsafe.inc.php里把 $imtypes = array ( "image/pjpeg", "image/jpeg", &q ...

  2. Flink-v1.12官方网站翻译-P004-Flink Operations Playground

    Flink操作训练场 在各种环境中部署和操作Apache Flink的方法有很多.无论这种多样性如何,Flink集群的基本构件保持不变,类似的操作原则也适用. 在这个操场上,你将学习如何管理和运行Fl ...

  3. 设计模式(二)——Java简单工厂模式

    简单工厂模式 案例: 披萨的项目(要便于披萨种类的扩展,要便于维护) 1)披萨的种类很多(比如 GreekPizz.CheesePizz 等) 2)披萨的制作有 prepare,bake, cut, ...

  4. 2020 CCPC-Wannafly Winter Camp Day2

    2020 CCPC-Wannafly Winter Camp Day2 A 托米的字符串 虽然每个子串出现的概率是相同的,但是同一长度的子串个数是不同的,所以要分别处理.计算出某一长度的情况下,元音字 ...

  5. 【poj 1988】Cube Stacking(图论--带权并查集)

    题意:有N个方块,M个操作{"C x":查询方块x上的方块数:"M x y":移动方块x所在的整个方块堆到方块y所在的整个方块堆之上}.输出相应的答案. 解法: ...

  6. uestc 1221 Ancient Go

    Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit  Status Y ...

  7. Codeforces Round #653 (Div. 3) A. Required Remainder (数学)

    题意:有三个正整数\(x,y,n\),再\(1\)~\(n\)中找一个最大的数\(k\),使得\(k\ mod\ x=y\). 题解:先记\(tmp=n/x\),再判断\(tmp*x+y\)的值是否大 ...

  8. 在Python中使用BeautifulSoup进行网页爬取

    目录 什么是网页抓取? 为什么我们要从互联网上抓取数据? 网站采集合法吗? HTTP请求/响应模型 创建网络爬虫 步骤1:浏览并检查网站/网页 步骤2:创建用户代理 步骤3:导入请求库 检查状态码 步 ...

  9. XSS脚本汇总

    (1)普通的XSS JavaScript注入<SCRIPT SRC=http://***/XSS/xss.js></SCRIPT> (2)IMG标签XSS使用JavaScrip ...

  10. 翻译:《实用的Python编程》01_04_Strings

    目录 | 上一节 (1.3 数字) | 下一节 (1.5 列表) 1.4 字符串 本节介绍处理文本的方法. 表示字面量文本 在程序中字符串字面量使用引号来书写. # 单引号(Single quote) ...