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 ...
随机推荐
- vue input框设置值 一般对象都是通过打点形式取值
- MAC os x 系统java开发环境搭建教程
https://jingyan.baidu.com/article/3d69c55147a3baf0cf02d7ca.html
- 重设SVN 的GNOME keyring [(null)] 的密码
在ubuntu里如果改了登录密码,那么在使用svn是会要求GNOME keyring [(null)] 的密码: 去掉这个只需:rm -f ~/.gnome2/keyrings/login.keyri ...
- [P1363] 幻想迷宫
题目链接 很好的一道搜索题,应该是利用了离散化的思想我好蒟蒻呀 地图是根据给定的图无限的拼接的. 所以说暴力建图是不可取的. 其实不难看出,在跨越两张图时.我们就可以看做这个点时空穿梭一般.从底下回来 ...
- HTML第二章:列表,表格,媒体元素
第二章:列表,表格,媒体元素 列表:有三种,有序列表,无序列表,定义列表 1.有序列表:<ol></ol> 列表项:<li></li&g ...
- 刚学不久的python,自己稍微加工了一点,影响不大,因为博主很懒,所以格式什么的,就没有太在意了!
本人初学python,之前因为有接触过其他语言,但是并没有接触的太深,出于对一个hacker的向往,学习一下Linux和python再说,当然,也是为了高工资啦,啊哈哈哈! 一开始就是一个蛮有意思的小 ...
- 关于ACL中通配符掩码(反掩码)认识
ACL(Access Control List) 访问控制列表在作为数据包的过滤器以及在对指定的某种类型的数据包的优先级,起到了对某些数据包的优先级起到了限制流量的作用,减少了网络的拥塞. ...
- 牛客小白月赛2 E 是是非非 【尼姆博弈】
链接:https://www.nowcoder.com/acm/contest/86/E来源:牛客网 题目描述 坎为水,险阳失道,渊深不测:离为火,依附团结,光明绚丽. 坎卦:水洊至,习坎:君子以常德 ...
- 搞定 mybatis generator 三步走
基于idea 编辑器下maven项目使用mybatis generator快速生成持久层 添加插件:插件网址:http://www.mybatis.org/generator/running/runn ...
- saltstack执行远程命令
目录 Remote Execution salt state salt state 系统 salt state 系统流程 Runner salt runner Orchestrate Runner S ...