PAT_A1102#Invert a Binary Tree
Source:
Description:
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
Keys:
Code:
/*
time: 2019-06-30 14:09:56
problem: PAT_A1102#Invert a Binary Tree
AC: 23:00 题目大意:
打印镜像树层序和中序遍历
输入:
第一行给出,结点数N<=10
接下来N行,结点i(0~n-1)的左孩子和右孩子 基本思路:
构造静态树遍历
*/
#include<cstdio>
#include<queue>
#include<string>
#include<iostream>
using namespace std;
const int M=1e2;
int mp[M]={},n;
struct node
{
int lchild,rchild;
}tree[M]; void LayerOrder(int root)
{
queue<int> q;
q.push(root);
int pt=;
while(!q.empty())
{
root = q.front();
q.pop();
printf("%d%c", root, ++pt==n?'\n':' ');
if(tree[root].rchild != -)
q.push(tree[root].rchild);
if(tree[root].lchild != -)
q.push(tree[root].lchild);
}
} void InOrder(int root)
{
if(root == -)
return;
static int pt=;
InOrder(tree[root].rchild);
printf("%d%c", root, ++pt==n?'\n':' ');
InOrder(tree[root].lchild);
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE scanf("%d", &n);
string r,l;
for(int i=; i<n; i++){
cin >> l >> r;
if(l == "-")
tree[i].lchild = -;
else{
tree[i].lchild = atoi(l.c_str());
mp[tree[i].lchild]=;
}
if(r == "-")
tree[i].rchild = -;
else{
tree[i].rchild = atoi(r.c_str());
mp[tree[i].rchild]=;
}
}
int root;
for(int i=; i<n; i++)
if(mp[i]==)
root=i;
LayerOrder(root);
InOrder(root); return ;
}
PAT_A1102#Invert a Binary Tree的更多相关文章
- 1102. Invert a Binary Tree (25)
The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...
- Invert a binary tree 翻转一棵二叉树
Invert a binary tree 翻转一棵二叉树 假设有如下一棵二叉树: 4 / \ 2 7 / \ / \ 1 3 6 9翻转后: 4 / \ 7 ...
- PAT1102: Invert a Binary Tree
1102. Invert a Binary Tree (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- PAT A1102 Invert a Binary Tree (25 分)——静态树,层序遍历,先序遍历,后序遍历
The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...
- A1102. Invert a Binary Tree
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 分) The following is from Max Howell @twitter: Google: 90% of our engine ...
- 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
The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...
- PAT甲级——A1102 Invert a Binary Tree
The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...
随机推荐
- 【LeetCode 4】寻找两个有序数组的中位数
题目链接 [题解] 假设在两个有序的序列中找第k小的数字. 那么我们先定位第一个序列中的第k/2个数字(不足则取最边上的那个数字)记下标为i1 然后定位第二个序列中的第k/2个数字(同样不足则取最边上 ...
- bzoj1066题解
[解题思路] 考虑拆点,把每根石柱拆成两个点,具体可以理解为石柱底部和石柱顶部,能爬到石柱顶部的蜥蜴只有有限只,而且蜥蜴只有爬到了石柱顶部才能跳到其他石柱的底部. 这样,考虑如下建图: 将每个有蜥蜴的 ...
- 笨办法学Python记录--习题37 异常,lambda,yield,转义序列
习题中提到了raise,查了下,顺便所有异常类关键字罗列如下文章中: 为什么使用异常 错误处理.事件通知.特殊情况处理.退出时的行为.不正常的程序流程. 简单的示例 在没有任何定义x变量的时候: pr ...
- 一个类似indexOf()的功能的函数
之前面试的时候遇到了这样的一道题,不过写的时候有些细节没注意到,现在重新写了一下. 写一个类似indexOf()的功能的函数 var str = "dafdfgvdahjfbhyuyvtur ...
- xshell突出显示集
xshell突出显示集(参考mobaxterm,直接拷贝过来不行,应该是xshell对正则表达式的支持不够好): Underline: \b(http(s)?://[A-Za-z0-9_./& ...
- springBoot使用PageHelper当超过最大页数后仍然返回数据
在SpringBoot中使用PageHelper分页插件时,如果设置pagehelper.reasonable=true时,pageNum<=0 时会查询第一页, pageNum>page ...
- 使用FTPClient实现文件上传服务器
import ch.qos.logback.classic.Logger; import org.apache.commons.net.ftp.*; import org.slf4j.LoggerFa ...
- POJ3630-Phone List-Trie字典树模板题
Given a list of phone numbers, determine if it is consistent in the sense that no number is the pref ...
- 力扣算法题—111.Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the sh ...
- 列表分成N等份
将一个长列表分为N个短列表 def Equal_division_list(eq_list, n): ''' :param seq:传入的列表 :param n:划分的份数,几等分 :return:返 ...