Source:

PAT A1099 Build A Binary Search Tree (30 分)

Description:

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 (≤) 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 − 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

Keys:

Code:

 /*
Data: 2019-06-26 16:22:18
Problem: PAT_A1099#Build A Binary Search Tree
AC: 21:23 题目大意:
BST定义:lchild < root <= rchild
给定一棵BST的树形,将所给序列填入BST中,并打印层次遍历
输入:
第一行给出,结点数N<=100
接下来N行,给出第i号结点,左孩子的序号,右孩子的序号(0~N-1,-1为空,0为根)
最后一行,N个数
输出:
层次遍历 基本思路:
建立静态树,构建根结点与左右孩子之间的关系
中序遍历静态树,由于BST的中序遍历就是从小到大递增的序列,把排序好的序列依次填入树的结点中
层次遍历并打印输出
*/
#include<cstdio>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
const int M=;
priority_queue<int,vector<int>,greater<int> > bst;
struct node
{
int data;
int lchild,rchild;
}tree[M]; void InOrder(int root)
{
if(root == -)
return;
InOrder(tree[root].lchild);
tree[root].data = bst.top();
bst.pop();
InOrder(tree[root].rchild);
} void Travel(int root, int len)
{
queue<int> q;
q.push(root);
while(!q.empty())
{
root = q.front();
q.pop();
printf("%d%c", tree[root].data, --len==?'\n':' ');
if(tree[root].lchild != -)
q.push(tree[root].lchild);
if(tree[root].rchild != -)
q.push(tree[root].rchild);
}
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE int n,x;
scanf("%d", &n);
for(int i=; i<n; i++)
scanf("%d %d", &tree[i].lchild,&tree[i].rchild);
for(int i=; i<n; i++)
{
scanf("%d", &x);
bst.push(x);
}
InOrder();
Travel(,n); return ;
}

最新文章

  1. linux 遇见的问题
  2. (lleetcode)Single Number
  3. Redis系列-存储篇list主要操作函数小结
  4. C++:类型转换
  5. android开发中遇到的问题
  6. Lambda表达式, 可以让我们的代码更优雅.
  7. Linux内核数据包的发送传输
  8. nginx的 CPU参数worker_processes和worker_cpu_affinity使用说明
  9. Android仿WIN8系统磁贴点击下沉倾斜效果
  10. HDOJ 5063 Operation the Sequence
  11. JAVA加密算法系列-BASE64
  12. hadoop集群中删除原有jdk设置
  13. Git 学习一
  14. require.js Javascript模块化
  15. C#编程(七十九)---------- 反射
  16. QOpenGLFunctions的相关的使用(1)
  17. 从零开始学Kotlin-操作符(3)
  18. MATLAB 的输入输出命令
  19. C++自定义异常类
  20. 关于Unity中的光照(一)

热门文章

  1. 【LeetCode 24】两两交换链表中的节点
  2. Linux shell脚本编程if语句的使用方法(条件判断)
  3. 傻瓜教程--------------linix上安装jdk
  4. drag事件
  5. A. Srdce and Triangle--“今日头条杯”首届湖北省大学程序设计竞赛(网络同步赛)
  6. 5、通过Appium Desktop实现页面元素定位
  7. 关于Unity中文件读取
  8. 2019杭电多校第一场hdu6579 Operation(线性基)
  9. Python脚本轻松实现批量图片重命名
  10. pytest--fixure前置执行一个函数