1102. Invert a Binary Tree (25)
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
#include<stdio.h>
#include<string>
#include<iostream>
#include<string.h>
#include<sstream>
#include<vector>
#include<map>
#include<stdlib.h>
#include<queue>
using namespace std; struct node
{
node():l(-),r(-){}
int l,r,id;
}; node Tree[];
bool notroot[];
bool fir = ;
void inoder(int root)
{
if(Tree[root].l != -)
{
inoder(Tree[root].l);
}
if(fir)
{
fir = ;
printf("%d",root);
}
else printf(" %d",root);
if(Tree[root].r != -)
{
inoder(Tree[root].r);
}
}
int main()
{
int n,tem;
char l[],r[];
scanf("%d",&n);
for(int i = ;i <n;++i)
{
Tree[i].id = i;
}
for(int i = ;i <n;++i)
{
scanf("%s%s",r,l);
if(l[] != '-')
{
tem = atoi(l);
Tree[i].l = tem;
notroot[tem] = ;
}
if(r[] != '-')
{
tem = atoi(r);
Tree[i].r = atoi(r);
notroot[tem] = ;
}
}
int root;
for(int i = ;i <n;++i)
{
if(!notroot[i])
{
root = i;
break;
}
}
queue<node> qq;
qq.push(Tree[root]);
bool fir2 = ;
while(!qq.empty())
{
node ntem = qq.front();
qq.pop();
if(fir2)
{
fir2 = ;
printf("%d",ntem.id);
}
else printf(" %d",ntem.id);
if(ntem.l != -)
{
qq.push(Tree[ntem.l]);
}
if(ntem.r != -)
{
qq.push(Tree[ntem.r]);
}
}
printf("\n");
inoder(root);
printf("\n");
return ;
}
1102. Invert a Binary Tree (25)的更多相关文章
- 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 ...
- PAT (Advanced Level) 1102. Invert a Binary Tree (25)
简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...
- PAT甲题题解-1102. Invert a Binary Tree (25)-(建树,水题)
就是把输入给的左孩子右孩子互换一下,然后输出层次遍历和中序遍历. #include <iostream> #include <algorithm> #include <c ...
- 【PAT甲级】1102 Invert a Binary Tree (25 分)(层次遍历和中序遍历)
题意: 输入一个正整数N(<=10),接着输入0~N-1每个结点的左右儿子结点,输出这颗二叉树的反转的层次遍历和中序遍历. AAAAAccepted code: #define HAVE_STR ...
- 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[比较简单]
1102 Invert a Binary Tree(25 分) The following is from Max Howell @twitter: Google: 90% of our engine ...
- 1102 Invert a Binary Tree——PAT甲级真题
1102 Invert a Binary Tree The following is from Max Howell @twitter: Google: 90% of our engineers us ...
- PAT 1102 Invert a Binary Tree
The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...
- 1102 Invert a Binary Tree (25 分)(二叉树遍历)
二叉树有N个结点,给出每个结点的左右孩子结点的编号,把二叉树反转(左右孩子交换 所以是后序遍历交换) 输出反转后二叉树的层序遍历和中序遍历 #include<bits/stdc++.h> ...
随机推荐
- 【Shell脚本学习12】Shell字符串
字符串是shell编程中最常用最有用的数据类型(除了数字和字符串,也没啥其它类型好用了),字符串可以用单引号,也可以用双引号,也可以不用引号.单双引号的区别跟PHP类似. 单引号 str='this ...
- 【转】Eclipse中一键调用javah生成jni的头文件
这里定义了一个本地方法jni_test,该方法返回一个String.其中System.loadLibrary是用来加载本地库的(dll或者so). 有了这个类以后,就可以调用javac命令编译,得到E ...
- 精通CSS高级Web标准解决方案(1-1选择器)
设计代码的结构 使用有意义的标记 css可以控制页面的外观并将表现与内容分隔开. 在分配ID与类名时尽量保证有意义且与表现无关. div可以对块级元素进行分组,而span可以对行内元素进行分组或标识. ...
- Sharepoint中有关文件夹的操作
1.GetItemsWithUniquePermissions根据返回数量和是否返回文件夹获取唯一权限的列表项集合 对于SharePoint对象模型中SPList的GetItemsWithUnique ...
- 破解软件感悟-PE文件格式之实例总结(五)
有很多介绍PE文件的文章,但是我打算写一篇关于输入表的文章,因为它对于破解很有用. 我想解释它的最好的方法是举一个例子,你可以跟着我逐步深入,一步一步的思考,最后你将完全明白,我选择了一个我刚 ...
- java开发命名规范(转载)
java开发命名规范 使用前注意事项: 1. 由于Java面向对象编程的特性, 在命名时应尽量选择名词 2. 驼峰命名法(Camel-Case): 当变量名或函式名是由一个或多个单字连结在一起,而 ...
- C# 线程--第一单线程基础
概念 什么是进程? 当一个程序被打开运行时,它就是一个进程.在进程中包括线程,进程可以由一个或多个线程组成. 什么是线程? 线程是程序执行流的最小单元.一个标准的线程由线程ID,当前指令指针(PC), ...
- Mac下github的使用
新建github账户 新建Repository,如下图: 建立连接github的秘钥 打开mac的shell cd ~ mkdir .ssh cd .ssh ssh-keygen -t rsa ...
- 九款酷炫基于jquery实现的应用及源码
1.HTML5 Loading动画加载 五彩的圆环Loading 今天我们要分享一款基于HTML5的Loading加载动画特效,这款HTML5加载动画是一个五彩的圆环,圆环不停地转动从而体现加载正在进 ...
- SSH 正向/反向代理小记
上周因为玩耍Minecraft的原因,折腾了下ssh的正向.反向代理,不得不说,科技改变命运..了解了基础的用法之后,很多跨域的事情都可以通过代理解决,而且只需要ssh帐号权限即可. 那么就简单来介绍 ...