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 ( ...
随机推荐
- js中的函数声明和函数表达式的区别
目录 一.声明与表达式的格式 1.1 声明式的格式: 1.2 表达式的格式: 二.区别 2.1 函数表达式可以直接在后面加括号执行,而函数声明不可以. 2.2 函数表达式可以被提前解析出来 2.3 命 ...
- sklearn中standardscaler中fit_transform()和transform()有什么区别,应该怎么使用?
在根据机器学习书中提供的实例中,看到需要对训练和测试的特征数据进行标准化. 但是使用的是有两个函数, 对于训练数据,使用的是fit_transform()函数 对于测试数据,使用的是tansform( ...
- linux 查看cpu,memory
https://www.cnblogs.com/ctypyb2002/p/9792951.html
- Centos7 安装 telnet 服务
准备写一个 django-webtelnet(运维管理系统集成后管理网络设备),但是手边没有现成的网络设备资源可以测试,那就研究下 Centos7 下安装 telnet-server 吧. 安装 yu ...
- 戏说 .NET GDI+系列学习教程(二、Graphics类的方法)
一.DrawBezier 画立体的贝尔塞曲线 private void frmGraphics_Paint(object sender, PaintEventArgs e) { Graphics g ...
- 10. Python面向对象
Python从设计之初就已经是一门面向对象的语言,正因为如此,在Python中创建一个类和对象是很容易的.如果接触过java语言同学应该都知道,Java面向对象三大特征是:封装.继承.多态.Pytho ...
- Java 反射获取私有方法
通常我们创建一个类时,它的私有方法在类外是不可见的,但是可以通过反射机制来获取调用.具体的反射机制的介绍大家自己百度. 所以反射可能会破坏我们的单例模式,当然解决方案也是有的,就是做个标记记录次数,第 ...
- jQuery 对象与 Dom 对象转化
首先,我们需要知道,为什么我们需要转化两者,原因在于,两者提供的方法不能共用. 比如: $("#id").innerHTML; document.getElementById(id ...
- hud 3183
题意:给出n个数字的字符串,要求你删除m个数字后,得到的数字最小. 分析:删除m个,就是选n-m个,而且,选的第一个数,肯定在(0—(n-m-1))中,第二个就在(第一个的下一位—(n-m-2)中.就 ...
- Java中++操作是同步的吗?为什么?
不是同步的 因为++操作分为三步实现 内存到寄存器 寄存器自增操作 寄存器写回内存 这三步每一步都可以被打断,不是原子操作,所以不是同步操作