二叉树有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 分)(二叉树遍历)的更多相关文章

  1. 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 ...

  2. 【PAT甲级】1102 Invert a Binary Tree (25 分)(层次遍历和中序遍历)

    题意: 输入一个正整数N(<=10),接着输入0~N-1每个结点的左右儿子结点,输出这颗二叉树的反转的层次遍历和中序遍历. AAAAAccepted code: #define HAVE_STR ...

  3. 1102. Invert a Binary Tree (25)

    The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...

  4. PAT (Advanced Level) 1102. Invert a Binary Tree (25)

    简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...

  5. PAT甲题题解-1102. Invert a Binary Tree (25)-(建树,水题)

    就是把输入给的左孩子右孩子互换一下,然后输出层次遍历和中序遍历. #include <iostream> #include <algorithm> #include <c ...

  6. PAT甲级——1102 Invert a Binary Tree (层序遍历+中序遍历)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90577042 1102 Invert a Binary Tree ...

  7. PAT 1102 Invert a Binary Tree[比较简单]

    1102 Invert a Binary Tree(25 分) The following is from Max Howell @twitter: Google: 90% of our engine ...

  8. 1102 Invert a Binary Tree——PAT甲级真题

    1102 Invert a Binary Tree The following is from Max Howell @twitter: Google: 90% of our engineers us ...

  9. PAT 1102 Invert a Binary Tree

    The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...

  10. 1110 Complete Binary Tree (25 分)

    Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...

随机推荐

  1. PHP设计模式练习——制作简单的投诉页面

    ---恢复内容开始--- <?php /* * 设计模式练习 * 1.数据库连接类(单例模式) * 2.调用接口实现留言本功能(工厂模式) * 3.实现分级举报处理功能(责任链模式) * 4.发 ...

  2. 如何提高mysql的安全性?

    1.如果 MySQL 客户端和服务器端的连接需要跨越并通过不可信任的网络,那么需要使用 ssh 隧道来加密该连接的通信.2.使用 set password 语句来修改用户的密码,先“mysql -u ...

  3. 中小学信息学奥林匹克竞赛-理论知识考点--ASCII

    ASCII表说白了就是一张表. 表中记录着:字符 和 数字 的对应关系.比如:字符0对应的ASCII码是48,A对应的是65,a对应的是97. 只要记住这三个,其它的数字,大写,小写字母的ASCII码 ...

  4. left join后面加上where条件浅析

    select a.*,b.* from table1 a left join table2 b on b.X=a.X where XXX 如上:一旦使用了left join,没有where条件时,左表 ...

  5. sup inf max min

    来自这里,觉得定义和举例都是最清楚的.http://www.math.illinois.edu/~ajh/347.summer14/completeness.pdf

  6. React后台管理系统-品类选择器二级联动组件

    1.页面大致是这个样子 2.页面结构 <div className="col-md-10">            <select name="&quo ...

  7. Java日志框架介绍

    一.序言 日志为系统的必不可少的一部分,通过输出的日志我们可以排查线上出现的各种问题,就像断案的线索一样.我们还可以通过日志数据分析用户的行为习惯做大数据分析. 二.日志框架分类及其历史 框架的种类: ...

  8. 经典sql语句汇总

    1,某条数据放首位,其他倒序并分页 select * from Student order by( case     when id='2'  then 1 ELSE 4 END),id desc l ...

  9. MySQL 5.7传统复制到GTID在线切换(一主一从)

    Preface       Classic replication is commonly used in previous version of MySQL.It's really tough in ...

  10. Linux下配置npm存放路径,解决权限问题

    1.打开cmd命令行,查看当前配置 输入 npm config ls 先看一下当前npm的配置环境,由于我已经修改过,所以可以看到修改后的路径 2.修改路径 这里需要修改两个路径,module路径和c ...