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

  • The left subtree of a node contains only nodes with keys less than the node's key.
  • The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
  • Both the left and right subtrees must also be binary search trees.

    Given the structure of a binary tree and a sequence of distinct integer keys, there is only one way to fill these keys into the tree so that the resulting tree satisfies the definition of a BST. You are supposed to output the level order traversal sequence of that tree. The sample is illustrated by Figure 1 and 2.



Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (<=100) which is the total number of nodes in the tree. The next N lines each contains the left and the right children of a node in the format "left_index right_index", provided that the nodes are numbered from 0 to N-1, and 0 is always the root. If one child is missing, then -1 will represent the NULL child pointer. Finally N distinct integer keys are given in the last line.

Output Specification:

For each test case, print in one line the level order traversal sequence of that tree. All the numbers must be separated by a space, with no extra space at the end of the line.

Sample Input:

9

1 6

2 3

-1 -1

-1 4

5 -1

-1 -1

7 -1

-1 8

-1 -1

73 45 11 58 82 25 67 38 42

Sample Output:

58 25 82 11 38 67 45 73 42

分析

这道题就是已知一个二叉搜索树的树形和节点的元素,叫你把元素填进去,然后输出层序遍历的结果,其实由二叉搜索树中序遍历的性质可知其中序遍历的结果是升序的,所以先把节点元素从小到大排序,这就是二叉搜索树的中序遍历的结果,再把二叉搜索树的节点下标按中序遍历输出与前面的元素一一对应就可以获得了二叉搜索树填进去的结果了,最后层序输出就OK了。

#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
struct child{
int left,right;
};
int n,t=0;
vector<child> children(100);
vector<int> a(100),b(100);
void inorder(int index){
if(children[index].left!=-1)
inorder(children[index].left);
b[index]=a[t++];
if(children[index].right!=-1)
inorder(children[index].right);
}
int main(){
int tag=0;
cin>>n;
for(int i=0;i<n;i++)
cin>>children[i].left>>children[i].right;
for(int i=0;i<n;i++)
cin>>a[i];
sort(a.begin(),a.begin()+n);
inorder(0);
queue<int> q;
q.push(0);
while(!q.empty()){
int temp=q.front();
q.pop();
tag++>0?cout<<" "<<b[temp]:cout<<b[temp];
if(children[temp].left!=-1)
q.push(children[temp].left);
if(children[temp].right!=-1)
q.push(children[temp].right);
}
return 0;
}

PAT 1099. Build A Binary Search Tree (树的中序,层序遍历)的更多相关文章

  1. PAT 1099 Build A Binary Search Tree[BST性质]

    1099 Build A Binary Search Tree(30 分) A Binary Search Tree (BST) is recursively defined as a binary ...

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

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

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

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

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

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

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

  7. PAT 甲级 1099 Build A Binary Search Tree

    https://pintia.cn/problem-sets/994805342720868352/problems/994805367987355648 A Binary Search Tree ( ...

  8. PAT A1099 Build A Binary Search Tree (30 分)——二叉搜索树,中序遍历,层序遍历

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

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

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

随机推荐

  1. C# winform通过按钮上移下移 解决了datasource绑定问题

    事件代码: private void btn_frmDicType_MoveUp_Click(object sender, EventArgs e) { int lstLength = this.ls ...

  2. P3469 [POI2008]BLO-Blockade tarjan

    好久没发博客了啊!自我反省1s...今天再捡起来. 这个题是一道有一点特殊的tarjan,用tarjan维护子树大小,然后判断是否有边多次连接,(就是非树边),然后就进行乘法计算就行了. 具体在代码里 ...

  3. 洛谷P1193 洛谷团队训练VS传统团队训练

    题目背景 "在中学的信息学教育领域,洛谷无疑是一个相当受欢迎的辅助网站.同时有百余所学校正在通过洛谷进行信息学竞赛(以后简称OI)的教育.洛谷之所以如此受欢迎,是因为洛谷创新的将OI教育的几 ...

  4. JSP-Runoob:JSP 表达式语言

    ylbtech-JSP-Runoob:JSP 表达式语言 1.返回顶部 1. JSP 表达式语言 JSP表达式语言(EL)使得访问存储在JavaBean中的数据变得非常简单.JSP EL既可以用来创建 ...

  5. Access restriction:The type JPEGCodec is not accessible due to restriction on required library C:\Program Files\Java\jre6\lib\rt.jar 报错

    报错: Access restriction:The type JPEGCodec is not accessible due to restriction on required library C ...

  6. 37. ext 中sm什么意思

    转自:https://zhidao.baidu.com/question/112450217.htmlsm是SelectionModel的缩写默认为RowSelectionModel其他模式还有Che ...

  7. 使用js模拟ecshop元素挪移

    <!DOCTYPE html><html><head> <title>移动</title> <script src="jqu ...

  8. ShaderLab中vertex fragment类Shader基础格式笔记

    //U3D用的shader语言叫ShaderLab,基础语法官方文档地址 //https://docs.unity3d.com/Manual/SL-Shader.html //开头指明名字,可以在别的 ...

  9. 洛谷P1396营救(最小生成树)

    题目描述 “咚咚咚……”“查水表!”原来是查水表来了,现在哪里找这么热心上门的查表员啊!小明感动的热泪盈眶,开起了门…… 妈妈下班回家,街坊邻居说小明被一群陌生人强行押上了警车!妈妈丰富的经验告诉她小 ...

  10. Linex系统 配置php服务器

    此文是可以参考 楼主也不是系统管理员只是迫不得已所以自己才找的  大家可以参考 .... ..... 安装apache 安装mysql 安装PHP 测试服务器 php -v 查询php的版本 就这些了 ...