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,然后依次输入当前结点的左孩子编号和右孩子编号,如果有孩子不存在用'-'来代替。然后反转这棵二叉树,输出反转过后的二叉树的层次遍历,和中序遍历。

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

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

#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甲级真题的更多相关文章

  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. CVE-2018-4407(IOS缓冲区溢出漏洞)exp

    CVE-2018-4407为ios缓冲区溢出漏洞 exp: import scapyfrom scapy.all import * send(IP(dst="同一局域网内目标Ip" ...

  2. java调用shell脚本执行操作

    //定时清空 日志 String shellString = "sh /home/jyapp/delete_log.sh"; Process process = Runtime.g ...

  3. Docker --volume(数据持久化)

    数据卷 volume 数据卷 是一个可供一个或多个容器使用的特殊目录,实现让容器中的一个目录和宿主机中的一个文件或者目录进行绑定.数据卷 是被设计用来持久化数据的 第一种:bind mount vol ...

  4. UML——构件图

    宏观导图: 细节探究: 一.What 构件:是一个应用很广的名词,在建筑工程.机械工程.软件工程中等都有该概念.其实,说道底表达的都是一个意思.就像是标准化生产出来的零部件一样,具有可替换性.同质性, ...

  5. 烧录失败导致boot无法加载的解决措施,再也不怕烧成砖了

    目录: 1.usb烧录时出现的问题截图 2.重新擦除boot发现失败的情况 3.解决措施 烧录失败导致boot无法加载的解决措施在烧录系统的时候经常会遇到烧录失败的情况,如果能通过再次执行烧录能烧上肯 ...

  6. Codeforces Round #640 (Div. 4)

    比赛链接:https://codeforces.com/contest/1352 A - Sum of Round Numbers 题意 将一个十进制数的每一个非零位分离出来. 代码 #include ...

  7. qmh的测试1

    题目:传送门 首先输入一个n,之后输入n个数a(1<=a<=1e7),对这n个数排序后,你需要找到所有的它们连续的长度.把这些连续的长度排序后输出 输入 输入: 8 1 5 2 7 4 5 ...

  8. AtCoder Beginner Contest 173 E - Multiplication 4 (思维)

    题意:有\(n\)个数,从中选\(k\)个数累乘,求最大的乘积\((mod\ 10^9+7)\). 题解: 1.假如全是负数,并且选奇数个,那么从小到大选. 2.否则,考虑当前状态,假如\(k\)是奇 ...

  9. AtCoder AIsing Programming Contest 2020 D - Anything Goes to Zero (二进制,模拟)

    题意:给你一个长度为\(n\)的\(01\)串,从高位到低位遍历,对该位取反,用得到的十进制数\(mod\)所有位上\(1\)的个数,不断循环直到为\(0\),输出每次遍历时循环的次数. 题解:根据题 ...

  10. jmespath(2)投影Projections

    投影 投影是JMESPath的关键特性之一.它允许您将表达式应用于元素集合.有五种投影: 列表投影 切片投影 对象投影 展平投影 过滤投影 处理投影需要注意的点 投影评估分为两个步骤.左侧(LHS)创 ...