二叉树有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. AI-Info-Micron-Insight:5G、人工智能和即将到来的移动革命

    ylbtech-AI-Info-Micron-Insight:5G.人工智能和即将到来的移动革命 1.返回顶部 1. 5G.人工智能和即将到来的移动革命 人们都说自己的手机“智能”,但究竟有多智能?凡 ...

  2. 彩色图像直方图均衡(Histogram Equalization)

    直方图均衡(Histogram Equalization) 一般步骤: 1.统计直方图每个灰度级出现的次数(概率) 2.累计归一化的直方图 3.计算新的像素值 重要:彩色直方图均衡不能对RGB分别做再 ...

  3. Extjs4.2 tabPosition left 相关

    解决tabPosition:left 标签的方向问题   <%@ page language="java" import="java.util.*" pa ...

  4. python中的zipfile

    zipfile - Work with ZIP archives ZipFile.namelist() Return a list of archive members by name. 返回压缩成员 ...

  5. 给大家一个我的QQ群

    很少关注博客了,提供一个QQ群讨论 我的一个QQ群:158351344

  6. MyString类的实现--基础中的基础C语言

    MyString 类是学习 C++ 的过程中一个很重要的例子,涉及到面向对象的封装.堆内存申请和释放.函数的重载以及 C++ 的 “Big Three”.本例子重点在于复习和理解上述的 C++ 特性, ...

  7. 简单了解:Web前端攻击方式及防御措施

    一.XSS [Cross Site Script]跨站脚本攻击  恶意攻击者往Web页面里插入恶意Script代码,当用户浏览该页之时,嵌入其中Web里面的Script代码会被执行,从而达到恶意攻击用 ...

  8. 堆(heap)和栈(stack)几点认识

    堆(heap)和栈(stack)主要的区别由以下几点:1.管理方式不同:2.空间大小不同:3.产生碎片不同:4.生长方向不同:5.分配归属不同:6.分配效率不同:7.存取效率不同:管理方式:对于栈来讲 ...

  9. 【杂题总汇】HDU多校赛第十场 Videos

    [HDU2018多校赛第十场]Videos 最后一场比赛也结束了…… +HDU传送门+ ◇ 题目 <简要翻译> 有n个人以及m部电影,每个人都有一个快乐值.每场电影都有它的开始.结束时间和 ...

  10. 控制器方法重复命名导致nginx 504的问题

    由于控制器方法重复命名重启swoole后运行代码导致 504 Gateway Time-out ,查看laravel日志和nginx日志才找原因所在,以后还是要多看错误日志.