1102 Invert a Binary Tree (25分)
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;建树完成后可以使用先序遍历或者是后序遍历进行树的反转,改变原来树的结构,随后就是简单的遍历操作啦。

#include<iostream>
#include<string>
#include<vector>
#include<queue>
using namespace std;
const int maxn = 100;
struct node {//二叉树静态写法int lchild=-1, rchild=-1;
}no[maxn];
int n;
int in_flag = 0, le_flag = 0;//在中序遍历和层序遍历时控制格式;
void postorder(int root)//后序遍历进行反转
{if (root==-1){return;}postorder(no[root].lchild);postorder(no[root].rchild);swap(no[root].lchild, no[root].rchild);//交换左右孩子,反转;
}
void inorder(int root)//中序遍历二叉树
{if (root == -1){return;}inorder(no[root].lchild);if (in_flag++ == 0)cout << root;else cout << " " <<root;inorder(no[root].rchild);
}
void level(int root)//层序遍历二叉树
{queue<int>q;q.push(root);int le_flag = 0;while (!q.empty()){int tmp = q.front();q.pop();if (le_flag++ == 0)cout << tmp;else cout << " " << tmp;if (no[tmp].lchild != -1)q.push(no[tmp].lchild);if (no[tmp].rchild != -1)q.push(no[tmp].rchild);}}
int main()
{int fa[11]{ 0 };//记录每个节点是否有父节点cin >> n;for (int i = 0; i < n; i++)//建立静态二叉树{string str1, str2;cin >> str1 >> str2;if (str1 != "-"){no[i].lchild = stoi(str1);fa[no[i].lchild] = 1;}if (str2 != "-"){no[i].rchild = stoi(str2);fa[no[i].rchild] = 1;}}int i = 0;for (i; i < n; i++)//寻找根节点,根节点的父节点不存在{if (fa[i] == 0){break;}}postorder(i);level(i);cout << endl;inorder(i);
}

也可以不对树进行改变,因为题目只是需要正确的输出就行,我们就可以只模拟输出。

#include<iostream>
#include<string>
#include<vector>
#include<queue>
using namespace std;
const int maxn = 100;
struct node {//二叉树静态写法int lchild=-1, rchild=-1;
}no[maxn];
int n;
int in_flag = 0, le_flag = 0;//在中序遍历和层序遍历时控制格式;
void inorder(int root)//不进行节点交换,先遍历右子树
{if (root==-1){return;}inorder(no[root].rchild);if (in_flag++ == 0)cout << root;else cout << " " << root;inorder(no[root].lchild);
}
void level(int root)//层序遍历二叉树
{queue<int>q;q.push(root);int le_flag = 0;while (!q.empty()){int tmp = q.front();q.pop();if (le_flag++ == 0)cout << tmp;else cout << " " << tmp;if (no[tmp].rchild != -1)q.push(no[tmp].rchild);//从右往左pushif (no[tmp].lchild != -1)q.push(no[tmp].lchild);//}}
int main()
{int fa[11]{ 0 };//记录每个节点是否有父节点cin >> n;for (int i = 0; i < n; i++)//建立静态二叉树{string str1, str2;cin >> str1 >> str2;if (str1 != "-"){no[i].lchild = stoi(str1);fa[no[i].lchild] = 1;}if (str2 != "-"){no[i].rchild = stoi(str2);fa[no[i].rchild] = 1;}}int i = 0;for (i; i < n; i++)//寻找根节点,根节点的父节点不存在{if (fa[i] == 0){break;}}level(i);cout << endl;inorder(i);}

1102 Invert a Binary Tree(甲级)相关推荐

  1. PAT甲级1102 Invert a Binary Tree:[C++题解]反转二叉树、递归

    文章目录 题目分析 题目链接 题目分析 反转二叉树这道题目特别出名!!!,是因为Homebrew这款Mac上大火的软件的作者到google面试,就考了这道题.面试官说了下面这段话:你竟然连在白板上翻转 ...

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

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

  3. PAT甲级-二叉树的遍历-1102 Invert a Binary Tree解题思路

    1102 Invert a Binary Tree (25 分) 思路 翻转二叉树 后序遍历翻转即可,由于给出每个结点的左右儿子,所以这里用到二叉树的静态写法更加方便 这里有个坑,bool数组初始化为 ...

  4. 1102 Invert a Binary Tree

    题目来源:PAT (Advanced Level) Practice The following is from Max Howell @twitter: Google: 90% of our eng ...

  5. 1102 Invert a Binary Tree (25point(s))

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

  6. 1102 Invert a Binary Tree (25 分)

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

  7. 1102. Invert a Binary Tree (25)-PAT甲级真题

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

  8. 1102 Invert a Binary Tree 需再做

    1. 题目的输入是,先给出结点总数N,然后N行给出的是值为x(0<=x<=N-1)的结点的左右结点的值,若不存在左/右结点,则值为 - . 2. 这一题我用动态链表没有做出来,根据参考书提 ...

  9. PAT甲级Invert a Binary Tree 柳神层序遍历的思路值得借鉴

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

最新文章

  1. java 线程交互_java 线程交互之wait/notify
  2. 2017.4.2号总结
  3. python编程基础与应用-Python编程基础与应用
  4. VMware安装MacOSx系统
  5. 修改 mybatis-generator 中数据库类型和 Java 类型的映射关系
  6. 构建项目时发生错误 - 错误:CS0006“... \ Assembly-CSharp-firstpass.dll”找不到
  7. php 扫描仪对接,Mac_Mac怎么连接扫描仪?苹果电脑Mac添加扫描仪教程,  有很多的用户需要用到扫 - phpStudy...
  8. 【笔记】Altera - Quartus II使用方法——工程创建、Modelsim破解/仿真、Verilog编写、举例(待续)
  9. 【校园卡】2020移动联通电信校园卡对比
  10. 口碑营销遇见互联网,企业如何做好网络口碑营销?
  11. ctfshow菜狗杯webshell wp
  12. cache的替换策略
  13. 随机生成10位数QQ号.c
  14. 【侯捷】C++STL标准库与泛型编程(第二讲)
  15. 石油大P14040存在
  16. php中文数组按拼音排序问题
  17. open3d学习教程1--点云对象PointCloud
  18. 百钱买百鸡问题java_Java版百钱买百鸡
  19. git push error: RPC failed; HTTP 403 curl 22 The requested URL returned error: 403
  20. P2P暴雷后续 完善互联网金融大数据风控模型成为命门

热门文章

  1. 安装tensorflow出现超时,找不到指定模+python 各个指定版本安装
  2. Failed to execute goal org.apache.maven.plugins:maven-resources-plugin
  3. 停止追赶最新的 RPA 趋势
  4. Android UI之困 横跨四个屏幕的战争
  5. 日报 18/06/25 26
  6. hdoj-1004-Let the Balloon Rise(map排序)
  7. SQuirreL SQL Client3.8 连接 HIVE2.2
  8. $.post()提交了数据,return不给跳转
  9. 模仿u-boot的makefile结构
  10. 20145324 《Java程序设计》第6周学习总结