Source:

PAT A1102 Invert a Binary Tree (25 分)

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的更多相关文章

  1. 1102. Invert a Binary Tree (25)

    The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...

  2. Invert a binary tree 翻转一棵二叉树

    Invert a binary tree 翻转一棵二叉树 假设有如下一棵二叉树: 4  / \   2    7  / \   / \ 1  3 6  9翻转后: 4     /    \    7 ...

  3. PAT1102: Invert a Binary Tree

    1102. Invert a Binary Tree (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  4. PAT A1102 Invert a Binary Tree (25 分)——静态树,层序遍历,先序遍历,后序遍历

    The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...

  5. A1102. Invert a Binary Tree

    The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...

  6. PAT 1102 Invert a Binary Tree[比较简单]

    1102 Invert a Binary Tree(25 分) The following is from Max Howell @twitter: Google: 90% of our engine ...

  7. PAT甲级——1102 Invert a Binary Tree (层序遍历+中序遍历)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90577042 1102 Invert a Binary Tree ...

  8. PAT 1102 Invert a Binary Tree

    The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...

  9. PAT甲级——A1102 Invert a Binary Tree

    The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...

随机推荐

  1. centos coreseek

    下载稳定版 coreseek wget http://www.coreseek.cn/uploads/csft/3.2/coreseek-3.2.14.tar.gz 解压 .tar.gz cd cor ...

  2. php获取网页源码分行显示

    file(PHP 3, PHP 4 )file -- 把整个文件读入一个数组中说明:file ( string filename [, int use_include_path [, resource ...

  3. LINIX上Nginx的从零安装

    源码编译方式: #一般系统中已经装了了make和g++,无须再装 安装make: yum -y install autoconf automake make 安装g++: yum -y install ...

  4. Codeforces ~ 1009C ~ Annoying Present (贪心)

    题意 一个长度为n的数组(初始全为0),进行m次操作. 操作:给你x,d,你任意挑选一个 i (1~n),每个数字加上 x+|i-j|*d( j 表示对应数字的下标) 问m次操作后的最大算术平均值为多 ...

  5. 计算1到N中包含数字1的个数

    转自:http://pandonix.iteye.com/blog/204840 Mark N为正整数,计算从1到N的所有整数中包含数字1的个数.比如,N=10,从1,2...10,包含有2个数字1. ...

  6. jmter 5.1 中文

    一.jmeter5.0下载解压后,默认的界面是英文版的,许多人觉得不方便,想要汉化,jmeter是不需要安装汉化包的,通过修改配置文件即可:1.找到jmeter解压后的文件夹,例如我是安装在D:\De ...

  7. Retrofit全攻略——进阶篇

    最近事比较多,距离上次写文章已经过去了一个月了.上一篇文章Retrofit全攻略——基础篇 介绍了Retrofit的基础用法,这篇文章介绍点进阶的用法. 打印网络日志 在开发阶段,为了方便调试,我们需 ...

  8. 3.2 Redux TodoApp

    上一节讲完了 redux 中的概念,但是仍然没有和 react 联系起来,这一节将利用 redux 在 react 中实现完整的 todolist: 在 react 使用 redux 通过 Provi ...

  9. JSON.toJSONString()null值转“”

    public static void main(String[] s) { CybWmsCommoditiesVo cybWmsCommoditiesVo = new CybWmsCommoditie ...

  10. 问题:Error running 'lugia-web': Address loaclhost:1099 is already in use

    解决方法:cmd输入下面命令: 第一步: netstat -ano|findstr 1099 找到对应的pid 为3576.(每次不一样). 第二步:taskkill -f -pid 3576