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> ...
随机推荐
- 终于也忍不住来写oi经历了
感觉好绝望. 突然间觉得这么长时间的oi学了就像没学一样,这么多的题做了就像没做一样. 努力付出,却不知希望在何处,也不知道该怎么办. 我好丧啊. 但是又没有办法 既然当初选择oi这条路 就只能继续走 ...
- 剑指offer--39. 跳台阶
时间限制:1秒 空间限制:32768K 热度指数:375795 题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果). cla ...
- JSP的EL和JSTL解析
1. EL 简介EL 全名为Expression Language,所有EL都是以${ 为起始.以} 为结尾的.EL 语法很简单,它最大的特点就是使用上很方便. 接下来介绍EL 主要的语法结构: ${ ...
- 如何让PPT播放时仅电脑显示备注页,而投影仪不显示
完全可以!第一步:在电脑上右键点击桌面选择属性,进入显示属性选着设置,点击2号屏(前提已连接投影仪或第2显示器),并且在“将WINDOS桌面扩展到改监视器上”(这个关键)前面打钩,且自主选择分辨率,应 ...
- 微信红包算法TEST
1.基本算法 设定总金额为10元,有N个人随机领取:N=1 则红包金额=X元: N=2 为保证第二个红包可以正常发出,第一个红包金额=0.01至9.99之间的某个随机数 第二个红包=10-第一个红包金 ...
- 完成users中的models
用户表中添加邮箱验证码数据表,轮播图数据表 from django.db import models from django.contrib.auth.models import AbstractUs ...
- Nodejs操作MongoDB数据库示例
//mongodb_demo.js /** cnpm install mongodb */ var MongoClient = require('mongodb').MongoClient; var ...
- IOS开发 多线程编程 - NSOperationQueue
一.简介 一个NSOperation对象可以通过调用start方法来执行任务,默认是同步执行的.也可以将NSOperation添加到一个NSOperationQueue(操作队列)中去执行,而且是异步 ...
- wireshark的提示
内容:12个wrieshark的提示 1.[Packet size limited during capture] 在捕获数据包大小有限,即包没有抓全 2.[TCP previous segment ...
- c++语言第二次作业
一题目7-1统计学生成绩 1实验代码 #include<stdio.h> int main(void) { int i,n,grade,A,B,C,D,E; A=B=C=D=E=; sca ...