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 ...
随机推荐
- Centos7 firewalld 基本使用
Centos7 的防火墙 firewalld比较常见 简单介绍使用 详细介绍链接推荐: https://blog.csdn.net/buster_zr/article/details/806049 ...
- inheritPrototypeChain.js
// 原型链 // 其基本思路是利用原型让一个引用类型继承另一个引用类型的属性和方法 function Person(){ this.name = "Person"; } Pers ...
- H5调拨打电话界面
<a href=”tel:15771791266 ”>拨打电话</a> 切记不要用js调用 直接用a标签 苹果安卓塞班都能调起来
- 2017-12-26 Java关键字的汉化用词探讨
@狗屎咖啡 的为GCC添加中文关键字项目对C关键词进行了汉化. 风格比较简约, 如'if'-如, 'else'-另. 个人感觉在中文编程语言尚未成熟之时, 不妨集思广益, 首先列出一些可选项, 然后从 ...
- 【读书笔记】iOS-应用内购买
Store Kit框架是一个应用内支付引擎.通过这个框架,付费应用可以实现用户付费购买内容的功能(比如为了获取额外的内容) 如果你发现Store Kit框架很难用,而且应用内付款不需要服务器端的支持, ...
- Ubuntu18.4中Apache在加不同端口的虚拟主机
1.添加监听端口 sudo vim /etc/apache2/ports.conf Listen 80 Listen 6080 <IfModule ssl_module> ...
- loadrunner 结果分析-loadrunner结果分析
结果分析-loadrunner结果分析 by:授客 QQ:1033553122 百度网盘分享链接: 烦请 复制一下网址到浏览器中打开,输入密码提取 链接: http://pan.baidu.com/s ...
- android系统通过图片绝对路径获取URI的三种方法
最近做项目要通过图片的绝对路径找到图片的URI,然后删除图片,小小总结一下获取URI的方法,亲自试验在 android 4.1.3的系统上都是可用的. 1.将所有的图片路径取出,遍历比较找到需要的路径 ...
- 利用Spring的AbstractRoutingDataSource解决多数据源的问题
多数据源问题很常见,例如读写分离数据库配置. 原来的项目出现了新需求,局方要求新增某服务器用以提供某代码,涉及到多数据源的问题. 解决方法如下: 1.首先配置多个datasource <bean ...
- 【第三篇】SAP ABAP7.5x新语法之程序结构&SubScreen
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文地址:SAP ABAP7.5x系列之程序结构& ...