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. IPSec 专题----转自华为文档

    参考链接:https://support.huawei.com/enterprise/zh/doc/EDOC1000122878?section=j004 IPSec 特性全景 1.介绍 由于IP报文 ...

  2. go语言常见面试题

    前言 从网上找了一些面试题,觉得有意思的我都记录下来,自己学习和大家一起学习吧. 有些初级的题目只放答案,一些值得探讨的问题我会写上我自己的分析过程,希望大家多多交流. 原文链接 选择题 1.[初级] ...

  3. 前端基础之html学习一:

    网站的建站流程 页面图例 网页的结构 WEB标准 WEB标准是网页制作的标准,它不是一个标准,它是根据网页的不同组成部分生成的一系列标准.这些标准大部分由W3C起草发布,也有部分标准由ECMA起草发布 ...

  4. 一统江湖的大前端(10)——inversify.js控制反转

    <大史住在大前端>前端技术博文集可在下列地址访问: [github总基地][博客园][华为云社区][掘金] 字节跳动幸福里大前端团队邀请各路高手前来玩耍,团队和谐有爱,技术硬核,字节范儿正 ...

  5. Jcrop图片裁剪

    一.引入js和css 二.实现 1.jsp页面 <%-- Created by IntelliJ IDEA. User: a Date: 2019/8/19 Time: 9:36 To chan ...

  6. Codeforces Global Round 9 B. Neighbor Grid

    题目链接:https://codeforces.com/contest/1375/problem/B 题意 给出一个 $n \times m$ 的方阵,每个方格中有一个非负整数,一个好方格定义如下: ...

  7. P1251 餐巾计划 (网络流)

    题意:餐厅每天会需要用Ri块新的餐巾 用完后也会产生Ri块旧的餐巾 每天购买新的餐巾单价p元 每天产出的旧餐巾可以送到快洗部花费每张c1元 在i + v1天可以使用 也可以花费c2元每张送到慢洗部 在 ...

  8. poj1061青蛙的约会 (扩展欧几里德)

    Description 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要的事 ...

  9. HDU-3081-Marriage Match II 二分图匹配+并查集 OR 二分+最大流

    二分+最大流: 1 //题目大意:有编号为1~n的女生和1~n的男生配对 2 // 3 //首先输入m组,a,b表示编号为a的女生没有和编号为b的男生吵过架 4 // 5 //然后输入f组,c,d表示 ...

  10. linux环境下使用jmeter进行分布式测试

    1.前言 熟练使用jmeter进行性能测试的工程师都知道,jmeter的客户端性能是有点差的.这会导致一个问题,其客户端的性能损耗会干扰到性能测试的结果,而且当线程数/并发大到一定程度时,客户端性能会 ...