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 ...
随机推荐
- Django + Mysql 中关于时间异常返回500错误的解决
问题描述: 最近在阿里云部署 Django(1.11.x) 时,在后台发布文章后,页面返回 500 异常. 刚开始的时候,遇到这个问题一脸懵逼,不知道该如何入手.后来把 settings.py 中 D ...
- 通过jQuery制作电子时钟表的代码
源码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <titl ...
- linux学习笔记-安装配置使用clamav杀毒软件
我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! 1.安装clamav 2.更新病毒库 # freshclam 如果更新不了,或者更新特别慢,可以手动下载病毒库文件,放到/var ...
- js+springMVC 提交数组数据到后台
1.ajax 代码 var ids =new Array(); $.ajax({ type: "POST", url: "/user/downReport", ...
- JavaSE——UDP协议网络编程(二)
在 UDP 网络编程中,发送方与接收方没有建立联系,没有明显的服务器端和客户端的区别. 类 DatagramSocket: 此类表示用来发送和接收数据报包的套接字. 主要的构造方法: Datagram ...
- struts2文件上传大小限制问题小结(引用)
最后解决办法: 页面js控制上传文件的大小,在页面进行控制.如下代码 inputs是所有文本上传input DOM //名称信息 var nameStr=''; //大小信息 var sizeStr= ...
- loadrunner 脚本开发-文件读写操作
脚本开发-文件读写操作 by:授客 QQ:1033553122 函数说明 函数原型: size_t fwrite( const void *buffer, size_t size, size_t co ...
- Android View体系(五)从源码解析View的事件分发机制
1.处理点击事件的方法 View的层级 我们知道View的结构是树形的结构,View可以放在ViewGroup中,这个ViewGroup也可以放到另一个ViewGroup中,这样层层的嵌套就组成了Vi ...
- [20171031]markhot.txt
[20171031]markhot.txt --//昨天看了https://jonathanlewis.wordpress.com/2017/10/02/markhot/,测试看看这样时候可以减少争用 ...
- 用LinQ扩展方法,泛型扩展方法,实现自定义验证字符是否空、对象是否为null,及泛型约束使用,Action的使用
一.Linq扩展方法 1.扩展方法必须是静态方法.扩展方法所在的类必须是静态类 2.扩展方法里面的参数必须制定this关键字,紧跟需要扩展的类型,如下: 二.泛型约束 1.使用泛型的原因,是在不知道需 ...