PAT甲题题解-1102. Invert a Binary Tree (25)-(建树,水题)
就是把输入给的左孩子右孩子互换一下,然后输出层次遍历和中序遍历。
#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)-(建树,水题)的更多相关文章
- 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 ...
- 1102. Invert a Binary Tree (25)
The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...
- PAT (Advanced Level) 1102. Invert a Binary Tree (25)
简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...
- 【PAT甲级】1102 Invert a Binary Tree (25 分)(层次遍历和中序遍历)
题意: 输入一个正整数N(<=10),接着输入0~N-1每个结点的左右儿子结点,输出这颗二叉树的反转的层次遍历和中序遍历. AAAAAccepted code: #define HAVE_STR ...
- PAT甲级——1102 Invert a Binary Tree (层序遍历+中序遍历)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90577042 1102 Invert a Binary Tree ...
- 1102 Invert a Binary Tree——PAT甲级真题
1102 Invert a Binary Tree The following is from Max Howell @twitter: Google: 90% of our engineers us ...
- PAT 1102 Invert a Binary Tree[比较简单]
1102 Invert a Binary Tree(25 分) The following is from Max Howell @twitter: Google: 90% of our engine ...
- PAT 1102 Invert a Binary Tree
The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...
- PAT甲题题解-1124. Raffle for Weibo Followers-模拟,水题
水题一个,贴个代码吧. #include <iostream> #include <cstdio> #include <algorithm> #include &l ...
随机推荐
- Apache的权限设置与构建虚拟web主机
实验拓扑图: 实验要求: 1. 搭建WEB服务器,能访问默认站点,并使用awstats软件能监控到默认站点的访问情况. 2. 修改Apache的主配置文件,设置1.10只能访问awstats网站, ...
- 乘风破浪:LeetCode真题_035_Search Insert Position
乘风破浪:LeetCode真题_035_Search Insert Position 一.前言 这次的问题比较简单,也没有限制时间复杂度,但是要注意一些细节上的问题. 二.Search Insert ...
- python3: 文件与IO
1.读写文本数据 # Write chunks of text data with open('somefile.txt', 'wt') as f: f.write(text1) # Redirect ...
- 树莓派3B+ wifi 5G连接
新烧的Raspbian 系统,一开始需要设置wifi的一些配置,其中会选择一个国家(set country),一开始选择的是CN(中国),发现只能连接2.4G的网络,5G的网络连接不上,这很奇怪, 因 ...
- Oracle RMAN 恢复数据库到不同主机(一)
一.RMAN 备份的内容 RMAN做数据库全备时包含了 数据文件.归档日志.控制文件和参数文件和备份日志,如下: arch_20160223_08qukp2t_1_1 arch_20160223_0 ...
- ViewData、ViewBag、TempData、Session的区别与联系
简介 这篇文章是我在学习ASP.NET MVC程序传值方式梳理总结的笔记.在ASP.NET MVC中,页面间和Controller与View之间主要有以下几种小量数据传值方式, ViewData.Vi ...
- 我的$OI$
我只是懒得写日记啦\(\color{pink}{qwq}\) //11月8日 啊--终于要\(NOIp\)了,为此期盼了好久.紧张了好久的我,不知道会迎来怎样的结果. 我只知道这段回忆是值得保留封存的 ...
- (转)公有云vr客户端tcp连接数太多造成 系统卡顿问题 [bittorrent tracker优化] -公有云常见网络问题及思路
在公有云服务器 发现使用tcp(http)的tracker连接数太多 用户太多会造成windows系统卡顿 特此发表一下修改配置和路由器的方法 解决卡顿问题 解决方法1(参考内容): 修改 /etc/ ...
- 【小程序】本地资源图片无法通过 WXSS 获取
小程序升级更改: 本地资源图片无法通过 WXSS 获取,可以使用网络图片,或者 base64,或者使用<image/>标签.请参考文档: https://mp.weixin.qq.com/ ...
- sql语句之表间字段值复制遇到的一些问题--基于mysql
好久没来园子了,转眼2017已经到3月份了,前段时间一直忙没时间写博客(其实是自己懒),感觉内心好惭愧.昨天临下班前,技术老大突然对我说要改下表结构,问我能不能实现将一个表的字段值复制到另外一个表的某 ...