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> ...
随机推荐
- OC-NSString
========================== 面向对象编程进阶和字符串 ========================== Δ一.类的设计模式—单例 [单例]程序允许过程中,有且仅有一块内存 ...
- MoreEffectiveC++Item35(异常)(条款9-15)
条款9 使用析构函数防止内存泄漏 条款10 在构造函数中防止内存泄漏 条款11 禁止异常信息传递到析构函数外 条款12 理解"抛出一个异常''与"传递一个参数"或调用一个 ...
- 关于Spinlock机制的一点思考
存在两段代码同时在多核上执行的情况,这时候才需要一个真正的锁来宣告代码对资源的占有. 几个核可能会同时access临界区,这时的spinlock是如何实现的呢? 要用到CPU提供的一些特殊指令,对lo ...
- flowable 五个引擎和组成引擎的服务
一.flowable的五个引擎 flowable包含五个引擎,分别是: 1.内容引擎 ContentEngine 2.身份识别引擎 IdmEngine 3.表单引擎 FormEngine 4.决策引擎 ...
- python爬虫入门(4)-补充知识:XPath 教程(转自w3school)
http://www.w3school.com.cn/xpath/index.asp 参考手册:http://www.w3school.com.cn/xpath/xpath_functions.asp ...
- python中类变量,成员变量
参考文献:http://www.jb51.net/article/54286.htm 转载.引用请附上参考文献的链接. (1)位置的区别 先看看下面这段代码: class TestClass(obje ...
- js学习笔记知识点
AJAX用法安全限制JSONPCORS面向对象编程创建对象构造函数原型继承class继承 AJAX 用法 AJAX不是JavaScript的规范,它只是一个哥们“发明”的缩写:Asynchronous ...
- Javascrpt 速成篇】 三:js事件处理
ie和chrome,firefox的事件处理,除了函数名字不同,基本大同小异.这样就已chrome为主了,对ie有兴趣的自己去百度.jquery已经处理不同浏览器兼容性问题,推荐使用. 事件处理有两种 ...
- Balanced Substring
You are given a string s consisting only of characters 0 and 1. A substring [l, r] of s is a string ...
- 如何使用cmd
cmd命令行 打开cmd 在windows操作系统中按住win+R键在弹出的窗口中输入cmd. 输入后按一下enter键,就进入了cmd命令行窗口. 打开磁盘文件 在命令行中输入你想要打开 ...