题目就是给出一棵二叉搜索树,已知根节点为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)-二叉树遍历的更多相关文章

  1. 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 ...

  2. pat 甲级 1099. Build A Binary Search Tree (30)

    1099. Build A Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...

  3. 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 ...

  4. 1099. Build A Binary Search Tree (30)

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

  5. PAT (Advanced Level) 1099. Build A Binary Search Tree (30)

    预处理每个节点左子树有多少个点. 然后确定值得时候递归下去就可以了. #include<cstdio> #include<cstring> #include<cmath& ...

  6. 【PAT甲级】1099 Build A Binary Search Tree (30 分)

    题意: 输入一个正整数N(<=100),接着输入N行每行包括0~N-1结点的左右子结点,接着输入一行N个数表示数的结点值.输出这颗二叉排序树的层次遍历. AAAAAccepted code: # ...

  7. PAT甲级——1099 Build A Binary Search Tree (二叉搜索树)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90701125 1099 Build A Binary Searc ...

  8. 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 ...

  9. pat1099. Build A Binary Search Tree (30)

    1099. Build A Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...

随机推荐

  1. 浏览器加载和渲染html的顺序-css渲染效率的探究(转载)

    1.浏览器加载和渲染html的顺序1.IE下载的顺序是从上到下,渲染的顺序也是从上到下,下载和渲染是同时进行的.2.在渲染到页面的某一部分时,其上面的所有部分都已经下载完成(并不是说所有相关联的元素都 ...

  2. [Eclipse]如何往eclipse中导入单个python文件,其它类型代码文件也可以参照该方法

    实例:想从外部单独拷一个文件到项目中指定路径,如果直接拷到对应文件夹路径下,启动eslipse又识别不到该文件,下面介绍直接copy的方法至eclipse,复制成功后即会在项目中对应路径下产生文件,下 ...

  3. 'No Transport' Error w/ jQuery ajax call in IE

    I need to use foursquare API to search venues. Of course it is cross-domain. It has no any problems ...

  4. BZOJ5368:[PKUSC2018]真实排名(组合数学)

    Description 小C是某知名比赛的组织者,该比赛一共有n名选手参加,每个选手的成绩是一个非负整数,定义一个选手的排名是:成绩不小于他的选手的数量(包括他自己). 例如如果333位选手的成绩分别 ...

  5. Kafka学习之路 (五)Kafka在zookeeper中的存储

    一.Kafka在zookeeper中存储结构图 二.分析 2.1 topic注册信息 /brokers/topics/[topic] : 存储某个topic的partitions所有分配信息 [zk: ...

  6. Python正则表达式操作指南(转)

    原文出处:http://www.amk.ca/python/howto/regex/ 适用版本:Python 1.5 及后续版本 摘要 本文是通过Python的 re 模块来使用正则表达式的一个入门教 ...

  7. linux固定ip地址

    最近自己搭jenkins发现ifconfig出来ip老是变来变去决定固定服务ip,原来配置: [root@bogon bin]# cat /etc/sysconfig/network-scripts/ ...

  8. day39

    今日内容: 1.对于表,库,记录的基本操作 2.数据库引擎的了解 3.表的详细 4.数据类型的掌握 1.回顾昨日对于表,库,记录的基本操作 库 增: create database mydb2; 删: ...

  9. awk、sed处理文件的简单例子

    awk.sed对处理日志文件和写shell脚本时非常有益.这个东西,如果不经常操作,真心过一段时间就忘差不多..要掌握熟练,就要多练习,这没什么可说的. awk '条件{命令}' filename 假 ...

  10. Redis Twemproxy

    主从复制+哨兵解决了读性能和高可用问题,但没有解决写性能问题. Twemproxy将写请求分配到不同节点处理. Twemproxy是Twitter开源的一个redis和memcache代理服务器. 允 ...