1102 Invert a Binary Tree——PAT甲级真题
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 6Sample Output:
3 7 2 6 4 0 5 1
6 5 7 4 3 2 0 1
题目大意:二叉树的每个结点按照从0~N - 1编号,先输入一个整数N,然后依次输入当前结点的左孩子编号和右孩子编号,如果有孩子不存在用'-'来代替。然后反转这棵二叉树,输出反转过后的二叉树的层次遍历,和中序遍历。
这道题因为最简单的字母输入卡了好久也是没谁了~~~。
大致思路:首先按照题目的要求进行读入构建二叉树,构建玩二叉树之后我们要找到二叉树的根节点。根据二叉树的性质可知,二叉树的根节点没有前驱,所以我们在读入的时候把有前驱的结点都标记一下然后找没有前驱的结点。然后递归反转二叉树,输出二叉树的层次遍历和中序遍历。
#include <bits/stdc++.h>
using namespace std;
const int N = 30;
struct TreeNode {
int id;
int left, right;
}root[N];
int n;
bool vis[N];
vector<int> levelorder, inorder;
void newNode(int x) {
root[x].id = x;
root[x].left = root[x].right = -1;
}
//递归的开始反转二叉树
void invert(int x) {
if (x == -1) return ;
swap(root[x].left, root[x].right);
invert(root[x].left);
invert(root[x].right);
}
void BFS(int x) {
queue<int> q;
q.push(root[x].id);
while(!q.empty()) {
int t = q.front(); q.pop();
levelorder.push_back(t);
if (root[t].left != -1) q.push(root[t].left);
if (root[t].right != -1) q.push(root[t].right);
}
}
void invist(int x) {
if (x == -1) return ;
invisit(root[x].left);
inorder.push_back(x);
invist(root[x].right);
}
int main() {
cin >> n;
memset(vis, 0, sizeof(vis));
for (int i = 0; i < n; i++) newNode(i);
char ch1, ch2;
for (int i = 0; i < n; i++) {
scanf("%c %c", &ch1, &ch2);
if (ch1 != '-') {
root[i].left = ch1 - '0';
vis[ch1 - '0'] = true;
}
if (ch2 != '-') {
root[i].right = ch2 - '0';
vis[ch2 - '0'] = true; //如果有指针指向这个点,则这个点不是根节点
}
}
int root_index;
for (int i = 0; i < n; i++) {
if (!vis[i]) {
root_index = i; //没有结点指向这个点,则这个点为根节点
break;
}
}
BFS(root_index); invist(root_index);
for (int i = 0; i < levelorder.size(); i++) {
cout << levelorder[i];
if (i != levelorder.size() - 1) cout << " ";
else cout << endl;
}
for (int i = 0; i < inorder.size(); i++) {
cout << inorder[i];
if (i != inorder.size() - 1) cout << " ";
else cout << endl;
}
return 0;
}
1102 Invert a Binary Tree——PAT甲级真题的更多相关文章
- PAT甲级——1102 Invert a Binary Tree (层序遍历+中序遍历)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90577042 1102 Invert a Binary Tree ...
- PAT 1102 Invert a Binary Tree[比较简单]
1102 Invert a Binary Tree(25 分) The following is from Max Howell @twitter: Google: 90% of our engine ...
- PAT 甲级真题题解(63-120)
2019/4/3 1063 Set Similarity n个序列分别先放进集合里去重.在询问的时候,遍历A集合中每个数,判断下该数在B集合中是否存在,统计存在个数(分子),分母就是两个集合大小减去分 ...
- PAT 1102 Invert a Binary Tree
The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...
- 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 ...
- 1102. Invert a Binary Tree (25)
The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...
- 1086 Tree Traversals Again——PAT甲级真题
1086 Tree Traversals Again An inorder binary tree traversal can be implemented in a non-recursive wa ...
- 1020 Tree Traversals——PAT甲级真题
1020 Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. Give ...
- PAT 甲级真题题解(1-62)
准备每天刷两题PAT真题.(一句话题解) 1001 A+B Format 模拟输出,注意格式 #include <cstdio> #include <cstring> #in ...
随机推荐
- python----类,面向对象(封装、继承、多态)(属性,方法)
什么是对象? 对象是内存中专门用来存储数据的一块区域 对象中可以存放各种数据(数字.代码等) 对象由三部分组成(1,对象标识(id)2,对象类型(type)3,对象的值(value)) 面向对象编程是 ...
- Educational Codeforces Round 83 D. Count the Arrays(组合,逆元,快速幂)
题意: 从 m 个数中选 n - 1 个数组成先增后减的长为 n 的数组. 思路: 因为 n 个数中有两个数相同,所以每种情况实际上只有 n - 1 个不同的数--$c_m^{n - 1}$, 除去最 ...
- 【uva 11093】Just Finish it up(算法效率--贪心)
题意:环形跑道上有N个加油站,编号为1~N.第 i 个加油站可以加油Ai加仑,从加油站 i 开到下一站需要Bi加仑汽油.问可作为起点走完一圈后回到起点的最小加油站编号. 解法:我们把每个加油站的Ai, ...
- hdu 6814 Tetrahedron 规律+排列组合逆元
题意: 给你一个n,你需要从1到n(闭区间)中选出来三个数a,b,c(可以a=b=c),用它们构成一个直角四面体的三条棱(可看图),问你从D点到下面的三角形做一条垂线h,问你1/h2的期望 题解: 那 ...
- 2020杭电多校 10C / HDU 6879 - Mine Sweeper (构造)
HDU 6879 - Mine Sweeper 题意 定义<扫雷>游戏的地图中每个空白格子的值为其周围八个格子内地雷的数量(即游戏内临近地雷数量的提示) 则一张地图的值\(S\)为所有空白 ...
- Slim Span POJ 3522 (最小差值生成树)
题意: 最小生成树找出来最小的边权值总和使得n个顶点都连在一起.那么这找出来的边权值中的最大权值和最小权值之差就是本题的结果 但是题目要求让这个输出的结果最小,也就是差值最小.那么这就不是最小生成树了 ...
- L2-019 悄悄关注 (25分) map容器模拟
代码: 1 //一道模拟水题,就用来给map练手吧 2 #include<stdio.h> 3 #include<string.h> 4 #include<iostrea ...
- 六、Python集合定义和基本操作方法
一.集合的定义方法及特点 1.特点: (1)由不同元素组成 #集合由不同元素构成 s={1,2,3,3,4,3,3,} print(s)#运行结果:{1, 2, 3, 4} (2)集合无序 #集合无序 ...
- Nginx基础 - HTTPS安全web服务
1.HTTPS配置语法 Syntax: ssl on | off; Default: ssl off; Context: http, server Syntax: ssl_certificate fi ...
- Nginx 版本回滚
目录 参考信息 源码安装 nginx-1.14.2 版本升级 nginx-1.16.1 版本回滚 ①.对于软件的版本升级.添加官方模块.添加第三方模块,都需要用源码安装包重新生成(configure) ...