PAT A1102 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 <algorithm>
#include <set>
#include <string.h>
#include <vector>
#include <queue>
using namespace std;
struct node{
int data;
int l=-,r=-;
};
const int maxn = ;
int n;
node tree[maxn];
int vis[maxn]={};
void lvl(int root){
queue<int> q;
q.push(root);
int cnt=;
while(!q.empty()){
int now=q.front();
q.pop();
cnt++;
if(cnt<n)printf("%d ",now);
else printf("%d\n",now);
if(tree[now].r!=-)q.push(tree[now].r);
if(tree[now].l!=-)q.push(tree[now].l);
}
}
int cnt=;
void ino(int root){
if(root==-)return;
if(tree[root].r!=-) ino(tree[root].r);
cnt++;
if(cnt<n)printf("%d ",root);
else printf("%d",root);
if(tree[root].l!=-) ino(tree[root].l);
}
int main(){
scanf("%d",&n);
getchar();
for(int i=;i<n;i++){
char c1,c2;
scanf("%c %c",&c1,&c2);
getchar();
int l=-,r=-;
if(c1!='-'){
l=c1-'';
vis[l]=;
}
if(c2!='-'){
r=c2-'';
vis[r]=;
}
tree[i].l=l;
tree[i].r=r;
tree[i].data=i; }
int root;
for(int i=;i<n;i++){
if(vis[i]==){
root=i;
break;
}
}
lvl(root);
ino(root);
}
注意点:又是读字符出现了错误,注意换行符一定要用getchar吃掉,不然会被%c认为是输入字符。别的就是普通的树的遍历
PAT A1102 Invert a Binary Tree (25 分)——静态树,层序遍历,先序遍历,后序遍历的更多相关文章
- 【PAT甲级】1102 Invert a Binary Tree (25 分)(层次遍历和中序遍历)
题意: 输入一个正整数N(<=10),接着输入0~N-1每个结点的左右儿子结点,输出这颗二叉树的反转的层次遍历和中序遍历. AAAAAccepted code: #define HAVE_STR ...
- PAT甲级——A1102 Invert a Binary Tree
The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...
- 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 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 (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
The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...
- 1110 Complete Binary Tree (25 分)
Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...
- PAT Advanced 1110 Complete Binary Tree (25) [完全⼆叉树]
题目 Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each ...
随机推荐
- 设计模式之迭代器模式(Iterator)
迭代器在STL运用广泛,类似容器的迭代已经成为其重要特性,而迭代器模式则是利用迭代器概念进行的抽象运用,迭代器模式运用广泛和有用,因为其能够不考虑数据的存储方式,而是直接面对数据进行迭代,也就是说我们 ...
- IOC容器的创建
一.IOC容器创建方式 Ioc容器的创建时通过ApplicationContext接口的相关实现类进行的. 如上图所示:有三种创建IOC容器的方式. ClassPathXmlApplicationCo ...
- Ansible安装 入门教程
learn一门新技术咯: ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.cfengine.chef.func.fabric)的优点,实现了批量系统配置 ...
- 测试思想-流程规范 SVN代码管理与版本控制
SVN代码管理与版本控制 by:授客 QQ:1033553122 欢迎加入软件性能测试交流群(QQ群):7156436 目录 一. 二. 三. 四. 五. 六. 七. 一. 创建根目录 创建一 ...
- 洗礼灵魂,修炼python(48)--巩固篇—模块
模块 其实前面都说过的,不过还是系统的再说一次,相信学到这,大部分都被搞忘了吧,所以再提一下,也为后面的博文做铺垫 1.什么是模块 在程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越 ...
- 几种流行的AJAX框架jQuery,Mootools,Dojo,Ext JS的对比
AJAX是web2.0的基石,现在网上流行几种开源的AJAX框架,比如:jQuery,Mootools,Dojo,Ext JS等等,那么我们到底在什么情况下该使用那个框架? 让我们来想想选择AJAX框 ...
- NPOI帮助类
/// <summary> /// NPOI导出帮助类 /// </summary> public class NPOIHelper { /// <summary> ...
- Centos 下添加开机自启动服务和脚本
最近刚玩Centos7的系统,跟Centos6还是很多方面有改变的,这里记录一下怎么在Centos7下添加开机自启动脚本和服务的方法. 1.添加开机自启服务 我这里以docker 服务为例,设置如下两 ...
- centos6.9设置桥接网络模式方法
第一步:设置 VMware 在 VMware 中打开[编辑]->[虚拟网络编辑器],添加 VMnet0,并选择桥接模式.需要注意的是,需要选择“桥接到”的网卡,使用无线网卡就选无线网卡,使用有线 ...
- DLL加载顺序
最近碰到了一个问题,要引入一个第三方的SDK,但是SDK中使用的一些dll和我原本程序里面有些dll是同名的,而且本程序的dll和sdk的dll名称都不能修改. 解决这个问题,首先想到的就是多进程,这 ...