【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 ...
随机推荐
- 记录 Docker 的学习过程 (安装基础篇)
docker 通过内核来实现 特点是效率高 1. centos7 三台(推荐2c 4g 最低 1c1g)2. 关闭防火墙 selinux3. 做好主机名解析,三台能互相ping通主机名host参考文件 ...
- Wannafly Camp 2020 Day 2F 采蘑菇的克拉莉丝 - 树链剖分
如果暴力维护,每次询问时需要对所有孩子做计算 考虑通过树剖来平衡修改与询问的时间,询问时计算重链和父树,轻链的贡献预先维护好,修改时则需要修改可能影响的轻链贡献,因为某个点到根的路径上轻重交替只有 \ ...
- Yii2 JWT
Yii2 JWT 这个扩展为Yii framework 2.0提供了JWT集成(需要PHP 5.6+).它包括基本的HTTP身份验证支持. 目录 安装 依赖关系 基本用法 创建 从字符串分析 验证 令 ...
- 使用饿了么el-date-picker里及如何将后台给的时间戳js转化为时间格式
首先代码是这个样子的,使用v-model <el-date-picker v-model="formData.createTime" :disabled="true ...
- static静态不是很静
在类中定义变量时,不会开辟存储空间,只有类定义一个对象时才会开辟类中成员变量的内存空间,且建立一个对象开辟一次,大小与类中的成员变量及函数有关.而static在静态区开辟内存空间,不占用内存空间. 1 ...
- Ecshop各个页面文件介绍,主要文件功能说明
1.模板文件说明 style.css – 模板所使用样式表activity.dwt – 活动列表article.dwt – 文章内容页article_cat.dwt – 文章列表页article_pr ...
- 解决Bootstrap container样式左右内边距15px,导致屏幕不美观
首先上问题:此问题为bootstrap的 container样式导致,该样式默认左右内边距15px为了栅栏效果而设计,具体看源码css样式,如下图,右侧黄色边框边距和30px,实为两个div左浮动,将 ...
- JS高级---案例:验证表单
案例:验证表单 <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...
- JS高级---正则表达式练习身份证号码
写正则表达式, 根据字符串来写正则表达式进行匹配 经验: 1.找规律 2.不要追求完美 身份证的正则表达式 15位或者18位 ([1-9][0-9]{14})|([1-9][0-9]{16}[0 ...
- 05 部署mysql关系数据库
01 安装Mysql 在官网https://dev.mysql.com/上找到自己需要的版本并执行安装 sudo apt-get install mysql-server-5.7 02 运行和退出 # ...