1102 Invert a Binary Tree (25 分)(二叉树遍历)
二叉树有N个结点,给出每个结点的左右孩子结点的编号,把二叉树反转(左右孩子交换 所以是后序遍历交换) 输出反转后二叉树的层序遍历和中序遍历
#include<bits/stdc++.h> using namespace std;
const int N=;
struct node
{
int L,R;
}s[N];
int toint(char ch)
{
if(ch=='-'){
return -;
}
else{
return ch-'';
} }
int isroot[N];
void postorder(int root)
{
if(root==-){
return;
}
postorder(s[root].L);
postorder(s[root].R);
swap(s[root].L,s[root].R);
}
vector<int>in;
void print(int root)
{
if(root!=-){
print(s[root].L);
in.push_back(root);
print(s[root].R);
}
}
vector<int>le;
void lever(int root)
{
queue<int>Q;
Q.push(root);
while(!Q.empty()){
int u=Q.front();
Q.pop();
le.push_back(u);
if(s[u].L!=-) Q.push(s[u].L);
if(s[u].R!=-) Q.push(s[u].R); }
}
int main()
{
fill(isroot,isroot+N,false);
int n;
scanf("%d",&n);
for(int i=;i<n;i++){
char ch1,ch2;
scanf("%*c%c %c",&ch1,&ch2);
int a=toint(ch1);
int b=toint(ch2);
s[i].L=a;
s[i].R=b;
isroot[a]=true;
isroot[b]=true;
}
int root=;
for(int i=;i<n;i++){
if(isroot[i]==false){
root=i;
}
}
postorder(root);
print(root);
lever(root);
for(int i=;i<le.size();i++){
if(i) printf(" ");
printf("%d",le[i]);
}
printf("\n");
for(int i=;i<in.size();i++){
if(i) printf(" ");
printf("%d",in[i]);
}
printf("\n");
return ;
}
1102 Invert a Binary Tree (25 分)(二叉树遍历)的更多相关文章
- 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 (25 分)(层次遍历和中序遍历)
题意: 输入一个正整数N(<=10),接着输入0~N-1每个结点的左右儿子结点,输出这颗二叉树的反转的层次遍历和中序遍历. AAAAAccepted code: #define HAVE_STR ...
- 1102. Invert a Binary Tree (25)
The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...
- PAT (Advanced Level) 1102. Invert a Binary Tree (25)
简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...
- PAT甲题题解-1102. Invert a Binary Tree (25)-(建树,水题)
就是把输入给的左孩子右孩子互换一下,然后输出层次遍历和中序遍历. #include <iostream> #include <algorithm> #include <c ...
- PAT甲级——1102 Invert a Binary Tree (层序遍历+中序遍历)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90577042 1102 Invert a Binary Tree ...
- 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——PAT甲级真题
1102 Invert a Binary Tree The following is from Max Howell @twitter: Google: 90% of our engineers us ...
- 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 ...
随机推荐
- Centos 5.2下安装多个mysql数据库
一.编译安装第一个MySQL 5.1.33 cd /opt/usr/sbin/groupadd mysql/usr/sbin/useradd -g mysql mysql -s /bin/nologi ...
- Hubtown(最大流)
Hubtown 时间限制: 1 Sec 内存限制: 128 MB提交: 23 解决: 11[提交] [状态] [讨论版] [命题人:admin] 题目描述 Hubtown is a large N ...
- Maven项目导出可执行jar
配置文件中添加插件 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>m ...
- 自己编写shave函数
import numpy def shave(I,border=None): I = I[border[0]:I.shape[0]-border[0],border[1]:I.shape[1]-bor ...
- svn更改地址怎么办
开发过程中有时会遇到服务器更换地址的情况,比如之前地址是 svn://www.aaa.com 后来换成了 svn://www.bbb.com 这时候怎么办呢?分客户端和服务器端2种情况处理 客户端: ...
- P2341 [HAOI2006]受欢迎的牛
P2341 [HAOI2006]受欢迎的牛 塔尔羊标准模板(我才不会告诉你我嘴里含着一个九省联考的出题人) 不会劈配.林克卡特树.制胡窜 我还会叉粪宿主,梳妆素组,西安段素 #include<c ...
- Fluent经典问题答疑
原文链接1 原文链接28 什么叫边界条件?有何物理意义?它与初始条件有什么关系? 边界条件与初始条件是控制方程有确定解的前提. 边界条件是在求解区域的边界上所求解的变量或其导数随时间和地点的变化规律. ...
- 关于vue中的nextTick深入理解
定义[nextTick.事件循环] nextTick的由来: 由于VUE的数据驱动视图更新,是异步的,即修改数据的当下,视图不会立刻更新,而是等同一事件循环中的所有数据变化完成之后,再统一进行视图更新 ...
- C#类的实例
一.C#类的实例 类的实例其实就是类的对象.定义类之后即可声明类的对象,声明对象之后便可以访问对象的成员了. 语法格式如下: 类名 对象名; // 声明对象对象名 = new 类名() ...
- 基于px2rpx-loader,探讨一下loader的封装思想
本文以px2rpx-loader的源码为学习对象,了解其工作机制以及loader封装的思想. 1.前言 最近在了解mpvue框架的时候,对于其能够实现一套代码兼容web和微信小程序(以下简称小程序)的 ...