题目就是给出一棵二叉搜索树,已知根节点为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. Win10更新后真正可用VC++6版本

    1.首先,我并不支持继续用VC6,毕竟太老太老了...除了VS,如果只是学C,那你完全可以用其它一些工具...当然除非你也是像我一样被逼无奈. 2.本次找了N多个版本,问题就是Win10周年更新包后, ...

  2. [luogu P4230]连环病原体

    [luogu P4230] 连环病原体 题意 给定一个长度为 \(n\) 的边序列, 当这个序列的一个子区间内的边都加入图中时产生了环则称其为"加强区间", 求序列中的每条边在多少 ...

  3. 不能用c99的情况下,如何动态定义数组的长度

    #include <stdio.h>#include <stdlib.h> int main(int argc, char const *argv[]){    int num ...

  4. sql优化常见的集中方法

    在sql查询中为了提高查询效率,我们常常会采取一些措施对查询语句进行sql优化,下面总结的一些方法,有需要的可以参考参考. 1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 ord ...

  5. java任务调度框架

    https://www.ibm.com/developerworks/cn/java/j-lo-taskschedule/

  6. 【转】android Toast大全(五种情形)建立属于你自己的Toast

    Toast用于向用户显示一些帮助/提示.下面我做了5中效果,来说明Toast的强大,定义一个属于你自己的Toast. 1.默认效果 代码 Toast.makeText(getApplicationCo ...

  7. leetcode 200. Number of Islands 、694 Number of Distinct Islands 、695. Max Area of Island 、130. Surrounded Regions

    两种方式处理已经访问过的节点:一种是用visited存储已经访问过的1:另一种是通过改变原始数值的值,比如将1改成-1,这样小于等于0的都会停止. Number of Islands 用了第一种方式, ...

  8. centos7上svn安装

    svn安装  yum install subversion 查看svn安装的版本  svnserve --version新建svn目录  mkdir /opt/svn  建立版本库目录  mkdir ...

  9. Python 函数(三)

    Python 3 函数 (闭包.装饰器.递归.高阶函数) 一.闭包 内部函数可以引用外部函数的参数和局部变量,当外部函数返回内部函数时,相关参数和变量 都保存在返回的函数中,简单的说,这种内部函数可以 ...

  10. C++之静态的变量和静态函数

    到目前为止,我们设计的类中所有的成员变量和成员函数都是属于对象的,如我们在前面定义的book类,利用book类声明两个对象Alice和Harry,这两个对象均拥有各自的price和title成员变量, ...