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 ...
随机推荐
- Flink-v1.12官方网站翻译-P019-Generating Watermarks
生成水印 在本节中,您将了解 Flink 提供的 API,用于处理事件时间时间戳和水印.关于事件时间.处理时间和摄取时间的介绍,请参考事件时间的介绍. 水印策略介绍 为了使用事件时间,Flink需要知 ...
- Hive之insert和insert overwrite
1. hive 表及数据准备 建表,并插入初始数据.向表中插入 hive> use test; hive> create table kwang_test (id int, name st ...
- Codeforces Round #678 (Div. 2)【ABCD】
比赛链接:https://codeforces.com/contest/1436 A. Reorder 题解 模拟一下这个二重循环发现每个位置数最终都只加了一次. 代码 #include <bi ...
- Codeforces Round #533 (Div. 2) C. Ayoub and Lost Array(递推)
题意: 长为 n,由 l ~ r 中的数组成,其和模 3 为 0 的数组数目. 思路: dp[ i ][ j ] 为长为 i,模 3 为 j 的数组数目. #include <bits/stdc ...
- Manacher算法 & Palindrome
马拉车用于解决最长回文子串问题,重点是子串,而不是子序列,时间复杂度为O(n). 解释一下变量的意义: Len[i]数组去存第i个位置到mx位置的长度 id记录上一次操作的位置(这个操作可以看模板) ...
- AcWing 238.银河英雄传说 (边带权并查集)
题意:有\(n\)列,有\(T\)条指令,若指令格式为\(M\),则将第\(i\)号的所有战舰移到第\(j\)号所在列的后面,若指令格式为\(C\),询问\(i\)和\(j\)是否在同一列,如果在,问 ...
- c++ 向上取整和向下取整
在c++ 中: ceil()表示向上取整 floor()表示向下取整 当然,这很显然对浮点数很好用. 但如果两个int类型的数想要向上取整呢? 我们用 (n-1)/m+1 来表示即可.
- Network POJ - 3694 无向图找桥
题意: 给你一个无向图,你需要找出来其中有几个桥 桥: 1.存在重边必定不为桥 2.low[v]>dfn[u] 代码: //题意很清晰 //就是这个需要先找出来原无向图中的桥个数,然后在判断添加 ...
- POJ1142 Smith Numbers 暴力+分解质因子
题意:题目定义了一个史密斯数,这个数的定义是:一个合数的各个位置上加起来的和等于它的素因数所有位置上的数字加起来的和.比如: 4937775=3∗5∗5∗658374+9+3+7+7+7+5=3+5+ ...
- 牛客编程巅峰赛S1第6场 - 黄金&钻石&王者 A.牛牛爱奇数 (模拟)
题意:有一组数,每次将所有相等的偶数/2,求最少操作多少次使得所有数变成奇数. 题解:用桶标记,将所有不同的偶数取出来,然后写个while模拟统计一下次数就行. 代码: class Solution ...