PAT甲题题解1099. Build A Binary Search Tree (30)-二叉树遍历
题目就是给出一棵二叉搜索树,已知根节点为0,并且给出一个序列要插入到这课二叉树中,求这棵二叉树层次遍历后的序列。
用结构体建立节点,val表示该节点存储的值,left指向左孩子,right指向右孩子。中序遍历的顺序正好是序列从小到大的顺序,因此中序遍历的时候顺便赋值就可以了,最后层次遍历输出。
思路一:中序遍历的时候赋值
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <queue>
#define LEFT 0
#define RIGHT 1
using namespace std;
const int maxn=;
int cnt=;
/*
中序遍历的顺序即为序列从小到大的顺序,因此中序遍历的时候顺便赋值,
最后层次遍历输出即可。
*/
struct Node{
int val;
int left;
int right;
}node[maxn]; void dfs(int i,int*a){
if(i==-)
return;
dfs(node[i].left,a);
node[i].val=a[cnt];
cnt++;
dfs(node[i].right,a);
} int main()
{
int n,l,r;
int a[maxn];
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d %d",&l,&r);
node[i].left=l;
node[i].right=r;
}
for(int i=;i<n;i++){
scanf("%d",&a[i]);
}
sort(a,a+n);
dfs(,a);
queue<Node>q;
q.push(node[]);
bool first=true;
while(!q.empty()){
Node tmp=q.front();
q.pop();
if(first){
printf("%d",tmp.val);
first=false;
}
else{
printf(" %d",tmp.val);
}
if(tmp.left!=-)
q.push(node[tmp.left]);
if(tmp.right!=-)
q.push(node[tmp.right]);
} return ;
}
思路二:两次dfs
这是我最开始的解题思路,复杂化了,不过好歹一次就AC了。
节点leftnum存储它的左子树的节点个数,rightnum存储它的右子树的节点个数,num存储以该节点为根节点的子树的节点个数。smallernum则是存储值比它小的节点个数。id代表了该节点是其父亲节点的左孩子还是右孩子,father是其父节点。
这样我们就能根据smallernum来判断该节点在序列(从小到大排列)中的位置。
第一次dfs把leftnum、rightnum、num给求出来。
第二次dfs则是计算smallernum,这里要分两种情况来考虑。
1.节点i为左孩子
那么往上追溯祖先节点,直到第一个id为右孩子的节点p,那么节点i的smallernum则为:
p的父亲节点的smallernum+1(即p的父亲节点)+节点i的左子树个数
2.节点i为右孩子
那么节点i的smallernum则为:
其父亲节点的smallernum+1(其父亲节点)+节点i的左子树个数。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <queue>
#define LEFT 0
#define RIGHT 1
using namespace std;
const int maxn=; struct Node{
int id;
int val;
int father;
int left;
int right;
int leftnum; //the number of left subtree
int rightnum;
int smallernum; //the number of left nodes,not only left subtree.
int num; //the number of this subtree
}node[maxn];
/*
calculate leftnum and num
*/
int dfsNum(int i){
if(i==-)
return ;
int l=node[i].left;
int r=node[i].right;
if(i==){
node[i].id=LEFT;
node[i].father=-;
}
if(l!=-){
node[l].id=LEFT;
node[l].father=i;
}
if(r!=-){
node[r].id=RIGHT;
node[r].father=i;
}
node[i].leftnum=dfsNum(l);
node[i].rightnum=dfsNum(r);
node[i].num=node[i].leftnum+node[i].rightnum+;
return node[i].num;
} void dfsSmallerNum(int i){
if(i==-)
return ;
int l=node[i].left;
int r=node[i].right;
dfsSmallerNum(l);
node[i].smallernum=;
if(node[i].id==LEFT){
int p=node[i].father;
while(p!=-){
if(node[p].id==RIGHT)
break;
p=node[p].father;
}
if(l!=-)
node[i].smallernum+=node[l].num;
if(p>){
node[i].smallernum+=node[node[p].father].smallernum+;
}
}
else{
if(l!=-)
node[i].smallernum+=node[l].num;
if(i>)
node[i].smallernum+=node[node[i].father].smallernum+;
}
dfsSmallerNum(r);
}
int main()
{
int n,l,r;
int a[maxn];
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d %d",&l,&r);
node[i].left=l;
node[i].right=r;
}
for(int i=;i<n;i++){
scanf("%d",&a[i]);
}
sort(a,a+n);
dfsNum();
dfsSmallerNum();
int res[maxn];
for(int i=;i<n;i++){
//printf("%d smaller:%d left:%d right:%d num:%d\n",i,node[i].smallernum,node[i].leftnum,node[i].rightnum,node[i].num);
int p=node[i].smallernum;
node[i].val=a[p];
}
queue<Node>q;
q.push(node[]);
bool first=true;
while(!q.empty()){
Node tmp=q.front();
q.pop();
if(first){
printf("%d",tmp.val);
first=false;
}
else{
printf(" %d",tmp.val);
}
if(tmp.left!=-)
q.push(node[tmp.left]);
if(tmp.right!=-)
q.push(node[tmp.right]);
} return ;
}
PAT甲题题解1099. Build A Binary Search Tree (30)-二叉树遍历的更多相关文章
- PAT (Advanced Level) Practise - 1099. Build A Binary Search Tree (30)
http://www.patest.cn/contests/pat-a-practise/1099 A Binary Search Tree (BST) is recursively defined ...
- pat 甲级 1099. Build A Binary Search Tree (30)
1099. Build A Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...
- PAT Advanced 1099 Build A Binary Search Tree (30) [⼆叉查找树BST]
题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...
- 1099. Build A Binary Search Tree (30)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- PAT (Advanced Level) 1099. Build A Binary Search Tree (30)
预处理每个节点左子树有多少个点. 然后确定值得时候递归下去就可以了. #include<cstdio> #include<cstring> #include<cmath& ...
- 【PAT甲级】1099 Build A Binary Search Tree (30 分)
题意: 输入一个正整数N(<=100),接着输入N行每行包括0~N-1结点的左右子结点,接着输入一行N个数表示数的结点值.输出这颗二叉排序树的层次遍历. AAAAAccepted code: # ...
- PAT甲级——1099 Build A Binary Search Tree (二叉搜索树)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90701125 1099 Build A Binary Searc ...
- 1099 Build A Binary Search Tree
1099 Build A Binary Search Tree (30)(30 分) A Binary Search Tree (BST) is recursively defined as a bi ...
- pat1099. Build A Binary Search Tree (30)
1099. Build A Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...
随机推荐
- November 12th, 2017 Week 46th Sunday
I love you not for who you are, but for who I am with you. 我爱你不是因为你是谁,而是因为跟你在一起,我是谁. I enjoy the fee ...
- XtraEditors六、ListBoxControl、CheckedListBoxControl、ImageListBoxControl
ListBoxControl 效果如下: 示例代码: string[] girlArr = { "面码", "Saber", "Mathilda&qu ...
- php 魔术方法 说明
1.__get.__set这两个方法是为在类和他们的父类中没有声明的属性而设计的.◆__get( $property ) 当调用一个未定义的属性时,此方法会被触发,传递的参数是被访问的属性名.◆__s ...
- BZOJ3251:树上三角形(乱搞)
Description 给定一大小为n的有点权树,每次询问一对点(u,v),问是否能在u到v的简单路径上取三个点权,以这三个权值为边长构成一个三角形.同时还支持单点修改. Input 第一行两个整数n ...
- JDBC中的SPI实现
DriverManger加载时候会调用如下方法: 关键代码:java.sql.DriverManager#loadInitialDrivers 然后 有时间整理一下
- layui小封装方法
//打开加载动画function LayerLoad() { layui.use('layer', function () { var layer = layui.layer; layer.load( ...
- yarn的学习-2-从 npm 迁移到 yarn-包管理工具
从npm处迁移过来多许多用户来说是一个相对简单的过程.yarn能想npm一样定制相同的package.json,并能够从npm仓库下载任意的包 如果你想要在已存在的npm项目中使用yarn,运行yar ...
- WorldWind源码剖析系列:相机类CameraBase
相机基类CameraBase PluginSDK中的相机类CameraBase是三维计算机图形学中的概念.观察者在三维场景中漫游时,通过眼睛看到的场景和相机拍摄过程非常一致.实际上,Direct3D和 ...
- JAVA框架Struts2 Action类
一.Action书写方式: 接口地址:https://struts.apache.org/maven/struts2-core/apidocs/index.html Action类就是一个POJO类. ...
- OC实现个人中心页面
AppDelegate.m: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDic ...