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> ...
随机推荐
- 为什么写《Tomcat内核设计剖析》
三四年前更多地还是做web业务开发,基本不关心web层以下的东西,但是每次出故障时面对现象都不能从脑子里形成由底层到应用层的完整的逻辑,往往只能分析到Web应用就无法继续往下,Web容器完全就是一个黑 ...
- 解决 src/MD2.c:31:20: fatal error: Python.h: No such file or directory安装包错误
在linux命令行安装包时报错 src/MD2.c:31:20: fatal error: Python.h: No such file or directory 原因:缺少了python的dev 解 ...
- Zabbix server 3.2安装部署
zabbix server 前提环境: CentOS 6 Lnmp php需要的包(bcmath,mbstring,sockets,gd,libxml,xmlwriter,xmlreader,ctyp ...
- Postfix常用命令和邮件队列管理(queue)
本文主要介绍一下postfix的常用命令及邮件队列的管理: Postfix有以下四种邮件队列,均由管理队列的进程统一进行管理: maildrop:本地邮件放置在maildrop中,同时也被拷贝到inc ...
- 为Linux服务器的SSH登录启用Google两步验证
对于Linux服务器而言使用密钥登录要比使用密码登录安全的多,毕竟当前网上存在多个脚本到处进行爆破. 这类脚本都是通过扫描IP端的开放端口并使用常见的密码进行登录尝试,因此修改端口号也是非常有必要的. ...
- javascript 小代码
if(!("a" in window)){ var a =1; } alert(a); //undefined var a = 1,b=function a (x){ x & ...
- Python IDE in Sublime
(最近换了电脑,然后忘了把 ST 的配置搬过来,所以重新折腾了一遍 Sublime 中的 Python 环境配置) 以下插件均通过 Package Control 安装. SublimeREPL 快捷 ...
- crt,excrt学习总结
\(crt,Chinese\ Remainder\ Theorem\) 概述 前置技能:同余基础性质,\(exgcd\). \(crt\),中国剩余定理.用于解决模数互质的线性同余方程组.大概长这样: ...
- Page View Controllers
Page View Controllers You use a page view controller to present content in a page-by-page manner. A ...
- windows中查看端口被什么应用程序占用并删除
windows中查看端口的命令是netstat,具体用法如下: 查看端口信息时可以使用如下命令: netstat -ano 运行结果如下: 当前我的本地13067端口被占用,使用命令如下: c:\&g ...