就是把输入给的左孩子右孩子互换一下,然后输出层次遍历和中序遍历。

#include <iostream>
#include <algorithm>
#include <cstring>
#include <string.h>
#include <cstdio>
#include <queue> using namespace std;
const int maxn=;
bool first=true;
struct Node{
int id;
int left;
int right;
}node[maxn];
int vis[maxn]; //没出现的即是根节点
//层次遍历
void level_order(int i){
queue<Node>q;
q.push(node[i]);
Node tmp;
while(!q.empty()){
tmp=q.front();
q.pop();
if(first){
first=false;
printf("%d",tmp.id);
}
else
printf(" %d",tmp.id);
int l=tmp.left;
int r=tmp.right;
if(l!=-)
q.push(node[l]);
if(r!=-)
q.push(node[r]);
}
}
//中序遍历
void in_order(int i){
if(i==-)
return;
int l=node[i].left;
int r=node[i].right;
in_order(l);
if(first){
printf("%d",i);
first=false;
}
else{
printf(" %d",i);
}
in_order(r);
}
int main()
{
int n;
char str1[],str2[];
memset(vis,,sizeof(vis));
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%s %s",str1,str2);
node[i].id=i;
if(str1[]=='-')
node[i].right=-;
else{
node[i].right=str1[]-'';
vis[str1[]-'']=;
}
if(str2[]=='-')
node[i].left=-;
else{
node[i].left=str2[]-'';
vis[str2[]-'']=;
}
}
int root;
for(int i=;i<n;i++){
if(!vis[i]){
root=i;
break;
}
}
first=true;
level_order(root);
printf("\n");
first=true;
in_order(root);
return ;
}

PAT甲题题解-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. 1102. Invert a Binary Tree (25)

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

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

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

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

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

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

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

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

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

  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. PAT 1102 Invert a Binary Tree

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

  9. PAT甲题题解-1124. Raffle for Weibo Followers-模拟,水题

    水题一个,贴个代码吧. #include <iostream> #include <cstdio> #include <algorithm> #include &l ...

随机推荐

  1. 使用 jekyll + github pages 搭建个人博客

    1. 新建 github.io 项目 其实 github pages 有两个用途,大家可以在官方网页看到.其中一个是作为个人/组织的主页(每个账号只能有一个),另一个是作为 github 项目的项目主 ...

  2. Hadoop HBase概念学习系列之HLog(二)

    首先,明确,HRegion服务器包含两大部分:HLog和HRegion. HLog用来存储数据日志,采用的是先写日志的方式. 当用户需要更新数据的时候,数据会被分配到对应的HRegion服务器上提交修 ...

  3. mysql workbench中my.ini路径不一样

    mysql workbench中的my.ini路径与mysql服务中的路径不一样 删除mysql workbench的配置文件即可解决 win7下的配置文件路径: %APPDATA\MySQL\Wor ...

  4. cJSON 使用详解

    由于c语言中,没有直接的字典,字符串数组等数据结构,所以要借助结构体定义,处理json.如果有对应的数据结构就方便一些, 如python中用json.loads(json)就把json字符串转变为内建 ...

  5. RLP(转发注明出处)

    目录 RLP序列化 什么是序列化? 为什么要序列化? RLP序列化处理的两项数据 RLP序列化采取的5项规则: 利用python写的RLP 实际中的使用是个怎么样子? RLP分析 参考目录 @ RLP ...

  6. Ecstore 默认图片压缩质量差的问题解决方法

    修改app/image/lib/clip.php文件 }elseif( function_exists('imagecopyresampled')){ $quality = 80; $image_p ...

  7. 宿主在Windows Service中的WCF(创建,安装,调用) (host到exe,非IIS)

    1. 创建WCF服务 在vs2010中创建WCF服务应用程序,会自动生成一个接口和一个实现类:(IService1和Service1) IService1接口如下:   using System.Ru ...

  8. 搞死人的contextRoot;weblogic9.2

    默认情况下: 两个app-deployment同时部署到了一台weblogic服务器的同一个domain下面的时候 /mysite/www/www/WEB-INF/weblogic.xml /mysi ...

  9. 静态工厂方法和实例工厂方法及普通的bean

    容纳你的bean  bean工厂:最简单的容器,提供了基础的依赖注入支持.创建各种类型的Bean.  应用上下文(ApplicationContext):建立在bean工厂基础之上,提供系统架构服务. ...

  10. Leetcode——413. 等差数列划分

    题目描绘:题目链接 题目中需要求解一个数组中等差数组的个数,这个问题可以利用动态规划的思路来分析. 三步骤: 1:问题归纳.题目需要求解等差数列的和,我们可以用一个数组保存前i个元素可以构成的等差数列 ...