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 ...
随机推荐
- Docker 安装MySQL5.7(三)
Docker 安装MySQL5.7 1.搜索docker镜像(可以看到搜索的结果,这个结果是按照一定的星级评价规则排序的) docker search mysql 2.拉取docker的mysql镜像 ...
- 【Java基础】4、java中的内部类
内部类的分类:常规内部类.静态内部类.私有内部类.局部内部类.匿名内部类. 实例1:常规内部类 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 2 ...
- springboot整合freemarker----一点小小的错误
最近小弟正在学习springboot,没办法,现在微服务太火了.小弟也要顺应时代的潮流啊 :( 好了,废话不多说了!!!! 首先在springboot的pom.xml添加freemarker的依赖 & ...
- homebrew 安装 formula 的不同历史版本——以安装 node 为例
homebrew 安装 formula 的不同历史版本--以安装 node 为例 系统环境 macOS Mojave 10.14 Homebrew 1.8.0 Homebrew/homebrew-co ...
- 谈谈web上各种图片应用的优缺点
web中承载信息的主要方式就是图片与文字了,以下就是对一些web图片格式的优缺点进行归纳. 1.GIF GIF图是比较古老的web图片格式之一,可以追溯到1987,几乎所有的浏览器都支持这一种格式,老 ...
- 用PHP去掉文件头的Unicode签名(BOM)
<?php //此文件用于快速测试UTF8编码的文件是不是加了BOM,并可自动移除 //By Bob Shen $basedir="."; //修改此行为需要检测的目录,点表 ...
- 我的Java之旅 第六课 JAVA WEB 请求与响应
一.有关URL编码 1.在URL的规范中定义了一些保留字符,如:: / ? & = @ % 等,在URI中有它的作用.如果要在URI中包含这些字符,必须转码,即%字符后跟十六进 ...
- Salesforce 小知识 - 必需字段
将字段定义为"必需" 当我们为对象设置字段的属性时,我们需要让某些字段在建立记录的时候必需有值,比如定义一个"地址"对象,那么必须填入"邮编" ...
- (后端)SQL Server日期时间函数
转自博客园: 1.获取当前日期GetDate getdate()函数以datetime数据类型的格式返回当前SQLServer服务器所在计算机的日期和时间.其语法格式为getdate().返回值舍入到 ...
- [OTA] 系统加密后Recovery是如何读取OTA升级包的
目前很多Android手机采用的FUSE方案,也就是内部SD卡不单独占用一个文件系统而实际上占用的是userdata的空间. 当系统加密后,解密需要VOLD的参于.而在Recovery模式下,是没有V ...