【PAT甲级】1102 Invert a Binary Tree (25 分)(层次遍历和中序遍历)
题意:
输入一个正整数N(<=10),接着输入0~N-1每个结点的左右儿子结点,输出这颗二叉树的反转的层次遍历和中序遍历。
AAAAAccepted code:
#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
int a[][];
bool vis[];
void bfs(int r){
queue<int>q;
q.push(r);
cout<<r;
while(!q.empty()){
int now=q.front();
q.pop();
if(a[now][]!=-){
cout<<" ";
cout<<a[now][];
q.push(a[now][]);
}
if(a[now][]!=-){
cout<<" ";
cout<<a[now][];
q.push(a[now][]);
}
}
}
int flag=;
void dfs(int r){
if(a[r][]!=-)
dfs(a[r][]);
if(flag)
cout<<" ";
cout<<r;
flag=;
if(a[r][]!=-)
dfs(a[r][]);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
for(int i=;i<;++i)
a[i][]=a[i][]=-;
int n;
cin>>n;
for(int i=;i<n;++i){
char x;
cin.ignore();
cin>>x;
cin.ignore();
if(x!='-')
a[i][]=x-'',vis[x-'']=;
char y;
cin>>y;
if(y!='-')
a[i][]=y-'',vis[y-'']=;
}
int root=;
for(int i=;i<n;++i)
if(!vis[i])
root=i;
bfs(root);
cout<<"\n";
dfs(root);
return ;
}
【PAT甲级】1102 Invert a Binary Tree (25 分)(层次遍历和中序遍历)的更多相关文章
- PAT甲级——1102 Invert a Binary Tree (层序遍历+中序遍历)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90577042 1102 Invert a Binary Tree ...
- 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 ...
- 【PAT甲级】1110 Complete Binary Tree (25分)
题意: 输入一个正整数N(<=20),代表结点个数(0~N-1),接着输入N行每行包括每个结点的左右子结点,'-'表示无该子结点,输出是否是一颗完全二叉树,是的话输出最后一个子结点否则输出根节点 ...
- PAT甲级——A1102 Invert a Binary Tree
The following is from Max Howell @twitter: Google: 90% of our engineers use the software 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 甲级 1066 Root of AVL Tree (25 分)(快速掌握平衡二叉树的旋转,内含代码和注解)***
1066 Root of AVL Tree (25 分) An AVL tree is a self-balancing binary search tree. In an AVL tree, t ...
- leetcode 题解:Binary Tree Inorder Traversal (二叉树的中序遍历)
题目: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary ...
- 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)-(建树,水题)
就是把输入给的左孩子右孩子互换一下,然后输出层次遍历和中序遍历. #include <iostream> #include <algorithm> #include <c ...
随机推荐
- 云服务器 使用 onedrive 快速同步
重大更新:支持微软的onedrive网盘,可以自动实时双向同步数据,也可以多台服务器和网盘之间实时同步数据.新增了一个虚拟环境python367,支持pytorch1.2:-----------微软O ...
- MySql 存储大量长字节 Text报错处理办法
今天线上版本的错误: Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Row size too large ...
- linux异常 - 网卡故障
问题描述: 弹出界面eth0: 错误:没有找到合适的设备:没有找到可用于链接System eth0 的设备 解决方案: 排错步骤如下: 1:查看系统是否识别相应网卡(发现没有eth0网卡存在): 根据 ...
- mysql 查询——逻辑查询
--去重查询 distinct select distinct gander from student; --逻辑查询 and or not --查询18-28之间的数据 select *from s ...
- python3练习100题——022
为了周末轻松点,多做一些题. 原题链接:http://www.runoob.com/python/python-exercise-example22.html 题目:两个乒乓球队进行比赛,各出三人.甲 ...
- python3练习100题——013
熟悉的水仙花数来了,,,... 原题链接:http://www.runoob.com/python/python-exercise-example13.html 题目:打印出所有的"水仙花数 ...
- android toolbar 显示返回按钮并改变按钮颜色
<android.support.design.widget.AppBarLayout android:id="@+id/about_appbar" android:layo ...
- python之路之线程,进程,协程2
一.线程 1.创建线程 2.主线程是否等待子线程 t.setDaemon(Ture/False):默认是false,等待子线程完成,ture,表示不等待子线程结束 3.主线程等待,子线程执行 join ...
- Virtual Judge POJ 2251 Dungeon Master
三维数组加宽搜 #include <stdlib.h> #include <string.h> #include <stdio.h> ; int c, k, h; ...
- DVWA全级别之File Inclusion(文件包含)
File Inclusion File Inclusion,意思是文件包含(漏洞),是指当服务器开启allow_url_include选项时,就可以通过php的某些特性函数(include(),req ...