1102 Invert a Binary Tree
题意:给定一个二叉树,要求输出翻转后的二叉树的层序序列和中序序列。
思路:不用真的翻转,只需要在输出时先访问右结点再访问左结点即可。
代码:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
using namespace std;
struct Node{
int left,right;
}tree[];
]={};
int n;
void levelOrder(int root)
{
;
queue<int> q;
q.push(root);
while(!q.empty()){
int u=q.front();
q.pop();
printf("%d",u);
cnt++;
if(cnt==n) printf("\n");
else printf(" ");
) q.push(tree[u].right);
) q.push(tree[u].left);
}
}
void inOrder(int root)
{
;
){
inOrder(tree[root].right);
printf("%d",root);
k++;
if(k==n) printf("\n");
else printf(" ");
inOrder(tree[root].left);
}
}
int main()
{
scanf("%d",&n);
],s2[];
;i<n;i++){
scanf("%s %s",s1,s2);
]==;
else {
tree[i].left=atoi(s1);
isRoot[tree[i].left]=;
}
]==;
else {
tree[i].right=atoi(s2);
isRoot[tree[i].right]=;
}
}
;
) root++;
levelOrder(root);
inOrder(root);
;
}
1102 Invert a Binary Tree的更多相关文章
- 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 (层序遍历+中序遍历)
本文同步发布在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 ...
- 1102. Invert a Binary Tree (25)
The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...
- PAT 1102 Invert a Binary Tree
The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...
- 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 (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 ...
- 1102 Invert a Binary Tree (25 分)(二叉树遍历)
二叉树有N个结点,给出每个结点的左右孩子结点的编号,把二叉树反转(左右孩子交换 所以是后序遍历交换) 输出反转后二叉树的层序遍历和中序遍历 #include<bits/stdc++.h> ...
随机推荐
- 用国内镜像源pip加速安装模块
记住,如果使用了virtualenv,一定要先workon进入虚拟环境再执行包安装命令. pip install -i https://pypi.douban.com/simple/ 模块名(如:dj ...
- js面向对象之:创建对象
最近在学习<js高级程序设计>,之前所接触的很多的js类库和jQuery插件都会用面向对象的方式来设计,而自己却还是停留在面向方法的阶段,所以今天好好记录一下学习的js创建对象. 第一种方 ...
- form表单序列化之后追加字段
方法是在{}中添加字段 key-value 一一对应,如下: var data = $.param({'state': state}) + '&' + $('#desProForm').ser ...
- Java 方法重载与方法重写
方法重载(Overload): 1.在同一个类中 2.方法名相同 3.参数的个数或类型不同 4.与方法的返回类型无关 5.与方法的修饰符无关 方法重写(Override): 方法重写必须是子类继承父类 ...
- onunload、onbeforeunload事件详解--zhuan
最近项目中做到一个功能:在上传页面用户开始上传文件之后用户点击任意跳转都需要弹出提示层进行二次确定才允许他进行跳转,这样做的目的是为了防止用户的错误操作导致这珍贵的UGC 流失(通常用户在一次上传不成 ...
- 浅析Symbol
不知道大家有没有留意ES6中的Symbol函数?在此之前,我对Symbol的认识知识这样的: 一.Symbol()和Symbol.for('str') Symbol()是独一无二的,你无法创建两个相 ...
- PHP常见带有下划线的常量
1.__PHP_Incomplete_Class <?php echo __PHP_Incomplete_Class::class; ?> __PHP_Incomplete_Class 2 ...
- (腾讯视频)iOS开发之视频根据url获取第一帧图片,获取任一帧图片
#import <AVFoundation/AVFoundation.h> + (UIImage*) thumbnailImageForVideo:(NSURL *)videoURL at ...
- fedora 26 Mysql
安装 Fedora用dnf默认安装的使Mariadb,即 [*****@localhost ~]$ sudo dnf install mysql-server ... [*****@localhost ...
- cnpm 私服搭建(基于docker)
备注: 使用docker-compose 进行安装 1. 代码clone git clone https://github.com/cnpm/cnpmjs.org.git 2. docker bu ...