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

#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. MySQL基础之 日期时间函数

    基础日期函数和时间函数 1.CURDATE()函数:返回当前只带有年月日格式的日期 2.CURTIME()函数:返回当前只带有时分秒格式的时间 3.NOW()函数:返回当前日期和时间 4.UNIX_T ...

  2. Django商城项目笔记No.16用户部分-用户中心收货地址

    首先完成省市区三级联动 新建areas应用 python ../../manage.py startapp areas 模型类代码 class Area(models.Model): "&q ...

  3. leetcode 6. ZigZag Conversion [java]

    自己写的: if(numRows == 1) return s; int ll = s.length() / 2 + 1; Character tc[] = new Character[numRows ...

  4. [BUG] python实例化N次类,调用类函数log会输出N遍的bug 解决办法

    最近再写DOU用例时,采用的是 unittest测试框架,就涉及到将其它所有模块需要全部在一个 .py文件中进行实例化,然后再运行时发现在控制台中同一个日志信息会打印多次(实例化几次,同一消息就会打印 ...

  5. (转)Python3异常-AttributeError: module 'sys' has no attribute 'setdefaultencoding

    基于python3.6.1版本,在一个.py文件中,加入这3行:import requests, re, sysreload(sys)sys.setdefaultencoding("utf- ...

  6. M100(3) 无线数传

    本文记录了无线数传配合地面Windos qt写的飞机控制SDK软件的使用过程 使用了两种类型无线数传 遇到问题 1 界面卡死问题 1.1 数传问题 问题描述: 使用非官方数传,点击active按键和一 ...

  7. 2.2.2 RelativeLayout(相对布局)

    本节引言 在上一节中我们对LinearLayout进行了详细的解析,LinearLayout也是我们 用的比较多的一个布局,我们更多的时候更钟情于他的weight(权重)属性,等比例划分,对屏幕适配还 ...

  8. day4-课堂笔记

    变量 成员变量-构造方法里定义 self.xxx 使用:类内部: self.xxx 类外部: 先创建实例 实例.xxx 类变量 类定义下面直接定义 使用:类方法内 cls.xxx 类名.xxx sel ...

  9. JS兼容各个浏览器的本地图片上传即时预览效果

    JS兼容各个浏览器的本地图片上传即时预览效果 很早以前 在工作曾经碰到这么一个需求,当时也是纠结了很久,也是google了很久,没有碰到合适的demo,今天特意研究了下这方面的的问题,所以也就做了个简 ...

  10. JavaScript中的箭头函数

    1.定义 箭头函数相当于匿名函数,并且简化了函数定义.箭头函数有两种格式,一种像上面的,只包含一个表达式,连{ ... }和return都省略掉了.还有一种可以包含多条语句,这时候就不能省略{ ... ...