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 ...
随机推荐
- 【Mysql】MySQL event 计划任务
一.查看event是否开启 show variables like '%sche%'; set global event_scheduler =1; 二. -- 设置时区并设置计划事件调度器开启,也可 ...
- netty 基础
Netty是一个高性能.异步事件驱动的NIO框架,提供了对TCP.UDP和文件传输的支持,作为一个异步NIO框架,Netty的所有IO操作都是异步非阻塞的,通过Future-Listener机制,用户 ...
- 《.NET之美》之程序集
一.什么是程序集(Assembly)? 经由编译器编译得到的,供CLR进一步编译执行的那个中间产物,在WINDOWS系统中,它一般表现为·dll或者是·exe的格式,但是要注意,它们跟普通意义上的WI ...
- python之网络通信协议
TCP/IP五层协议和OSI的七层协议: TCP和UDP的区别: Tcp协议:面向连接,数据可靠,传输效率低,面向字节流 Udp协议:面向无连接,数据不可靠,传输效率高,面向报文
- spring 开发 Tars
和不使用 Spring 的 tars HelloWord 项目相比,客户端完全一样,服务端两个地方不一样 创建不使用 Spring 的 tars HelloWord 步骤: https://www.c ...
- 安卓开发_浅谈Notification(通知栏)
Notification通知栏是显示在手机状态的消息,代表一种全局效果的通知 快速创建一个Notification的步骤简单可以分为以下四步: 第一步:通过getSystemService()方法得到 ...
- Flutter 布局(七)- Row、Column详解
本文主要介绍Flutter布局中的Row.Column控件,详细介绍了其布局行为以及使用场景,并对源码进行了分析. 1. Row A widget that displays its children ...
- (后端)SQL Server日期时间函数
转自博客园: 1.获取当前日期GetDate getdate()函数以datetime数据类型的格式返回当前SQLServer服务器所在计算机的日期和时间.其语法格式为getdate().返回值舍入到 ...
- Django 拾遗
1.python_2_unicode_compatible装饰器 from django.db import models from django.utils.encoding import pyth ...
- [20171225]变态的windows批处理4.txt
[20171225]变态的windows批处理4.txt --//昨天学习windows 批处理的echo &.使用它可以实现类似回车换行的功能.例子: 1.echo &.R:\> ...