PAT甲级——1102 Invert a Binary Tree (层序遍历+中序遍历)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90577042
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
题目大意:将二叉树每个节点的左右孩子交换位置,然后分别层序和中序输出。第 i 行代表了节点 i 的左右孩子的信息,先左后右,'-' 表示没有该方向的子节点。
思路:用数组tree存储树。
读取字符串然后将其转成int型变量,空节点'-' 用-1代替。
只要在读取数据的时候交换左右孩子的位置就行。
读取数据的时候用bool数组R对每个孩子节点进行标记,然后遍历数组寻找根节点(即没有被标记过的节点)
1 #include <iostream> 2 #include <vector> 3 #include <string> 4 #include <queue> 5 using namespace std; 6 struct node { 7 int left, right; 8 }; 9 vector <node> tree; 10 bool flag = false;//用于中序遍历标记第一个输出的节点 11 int getNum(string &s); 12 void levelOrder(int t); 13 void inOrder(int t); 14 int main() 15 { 16 int N, root; 17 scanf("%d", &N); 18 tree.resize(N); 19 vector <bool> R(N, true); 20 for (int i = 0; i < N; i++) { 21 string left, right; 22 cin >> left >> right; 23 tree[i].left = getNum(right); 24 tree[i].right = getNum(left); 25 if (tree[i].left != -1) 26 R[tree[i].left] = false; 27 if (tree[i].right != -1) 28 R[tree[i].right] = false; 29 } 30 for (int i = 0; i < N; i++) 31 if (R[i]) { 32 root = i; 33 break; 34 } 35 levelOrder(root); 36 printf("\n"); 37 inOrder(root); 38 printf("\n"); 39 return 0; 40 } 41 void inOrder(int t) { 42 if (t != -1) { 43 inOrder(tree[t].left); 44 if (flag) 45 printf(" "); 46 if (!flag) 47 flag = true; 48 printf("%d", t); 49 inOrder(tree[t].right); 50 } 51 } 52 void levelOrder(int t) { 53 queue <int> Q; 54 Q.push(t); 55 while (!Q.empty()) { 56 t = Q.front(); 57 printf("%d", t); 58 Q.pop(); 59 if (tree[t].left != -1) { 60 Q.push(tree[t].left); 61 } 62 if (tree[t].right != -1) { 63 Q.push(tree[t].right); 64 } 65 if (!Q.empty()) 66 printf(" "); 67 } 68 } 69 int getNum(string& s) { 70 if (s[0] == '-') 71 return -1; 72 int n = 0; 73 for (int i = 0; i < s.length(); i++) 74 n = n * 10 + s[i] - '0'; 75 return n; 76 }
转载于:https://www.cnblogs.com/yinhao-ing/p/10926383.html
PAT甲级——1102 Invert a Binary Tree (层序遍历+中序遍历)相关推荐
- PAT甲级1102 Invert a Binary Tree:[C++题解]反转二叉树、递归
文章目录 题目分析 题目链接 题目分析 反转二叉树这道题目特别出名!!!,是因为Homebrew这款Mac上大火的软件的作者到google面试,就考了这道题.面试官说了下面这段话:你竟然连在白板上翻转 ...
- 1102 Invert a Binary Tree
题目来源:PAT (Advanced Level) Practice The following is from Max Howell @twitter: Google: 90% of our eng ...
- 1102. Invert a Binary Tree (25)-PAT甲级真题
The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...
- PAT甲级-二叉树的遍历-1102 Invert a Binary Tree解题思路
1102 Invert a Binary Tree (25 分) 思路 翻转二叉树 后序遍历翻转即可,由于给出每个结点的左右儿子,所以这里用到二叉树的静态写法更加方便 这里有个坑,bool数组初始化为 ...
- 1102 Invert a Binary Tree(甲级)
1102 Invert a Binary Tree (25分) The following is from Max Howell @twitter: Google: 90% of our engine ...
- 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 ...
- 1102 Invert a Binary Tree (25 分)
1102 Invert a Binary Tree (25 分) The following is from Max Howell @twitter: Google: 90% of our engin ...
- 二叉树(Binary Tree):先序遍历、中序遍历、后序遍历和层次遍历
二叉树(Binary Tree):先序遍历.中序遍历.后序遍历和层次遍历 树 Tree 二叉树 Binary Tree 先序遍历 Preorder Traversal 中序遍历 Inoreder Tr ...
- 通过层序和中序遍历序列重建二叉树
在学二叉树的重建时,在<算法笔记>上学到了如何通过先序(或后序)遍历序列和中序遍历序列重建二叉树,它也提出了一个问题:如何通过层序和中序遍历序列重建二叉树?我一开始按照先序和中序重建的思路 ...
最新文章
- JAVA实现中点画线_实验1-中点画线和Bresenham画线算法的实现
- 秒杀系统架构解密与防刷设计 - 高可用架构系列
- 毕业设计:基于Web实现多用户宿舍管理系统
- 【代码真相】函数调用 堆栈
- qt4 与php 通讯,Qt与外部程序通信问题
- dotnet core入门
- flutte的第一个hello world程序
- spark 2.2 读取 Hadoop3.0 数据异常 org.apache.hadoop.hdfs.web.HftpFileSystem cannot access its superinterfa
- 大数据可视化html模板开源_8个可靠好用的开源数据可视化工具推荐
- 基于asp.net的网上书店商城设计与实现
- 20日直播预告丨一则ORA-600案例分析
- JS判断图片是否加载完毕
- 元素周期表排列的规律_元素周期表的秘密【机密文件】
- Facebook的新算法可以预测出你的贫富阶级
- 【verbs】ibv_query_port()
- Excel如何筛选数据重复项?
- 程序包不存在 错误解决方法
- Oracle又裁员,甲骨文在云计算市场再难突破?
- Python numpy函数:shape用法(简单样本及样本对应变量场景)
- 周期信号的傅里叶级数展开