二叉树有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. 2017.11.16 JavaWeb-------第八章 EL、JSTL、Ajax技术

    第八章 EL.JSTL.Ajax技术 ~~ EL (expression language) 是表达式语言 ~~ JSTL(JSP Standard Tag Library) 是开源的JSP标准标签库 ...

  2. N76E003---看门狗

    看门狗的设置 比较简单,根据芯片手册上的说明进行设置.值得一提的是设置看门狗的寄存器是保护寄存器,所以在写寄存器的时候要解除保护 void wtd_init(void) { TA=0xAA; TA=0 ...

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

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

  4. C# foreach语句

    一.C# foreach语句 foreach语句能够对实现Ienumerable接口的容器进行遍历,并提供一个枚举器来实现Ienumerable接口.foreach语句为数组或对象集合中的各个元素执行 ...

  5. js动画效果

    js能够按照预定的时间间隔重复调用一个函数,而这意味着我们可以随着时间的推移而不断改变某个元素的样式.动画是样式随时间变化的完美例子之一. js函数setTimeout能够让某个函数在经过一段预定的时 ...

  6. 总结ing

    1,iOS的GCD中如何关闭或者杀死一个还没执行完的后台线程? 举例来说,我通过导航进入到了一个视图,这个视图加载的时候会新建一个线程在后台运行,假设这个线程需要从网络中读取许多数据,需要一定的时间, ...

  7. SSH & 文件传输 & 远程桌面管理

    SSH   Windows Linux MacOS Android IOS                                     https://www.ssh.com http:/ ...

  8. ajax 传递文件成功时 jQuery提示parsererror错误

    后台返回值类型 改为:PrintWriter out = response.getWriter();String jsonStr = "{\"success\":\&qu ...

  9. JDK及配置

    Java Jdk开发时环境,程序员使用 Jre运行时环境,用户使用 Jdk的配置 1.新建java_home   jdk的安装路径 例:C:\Program Files (x86)\Java\jdk1 ...

  10. 正则表达式,regular expression, regex, RE

    正则表达式是用来简洁表达一组字符串的表达式 正则表达式可以用来判断某字符串的特征归属