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 ;
}

PAT_A1099#Build A Binary Search Tree的更多相关文章

  1. PAT1099:Build A Binary Search Tree

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

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

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

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

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

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

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

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

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

  7. PAT-1099(Build A Binary Search Tree)Java实现+二叉排序树的中序遍历和层次遍历

    Build A Binary Search Tree PAT-1099 本题有意思的一个点就是:题目已经给出了一颗排序二叉树的结构,需要根据这个结构和中序遍历序列重构一棵二叉排序树. 解法:可以根据中 ...

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

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

  9. A1099. Build A Binary Search Tree

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

随机推荐

  1. 4-基于DoG的特征检测子(SIFT:稳定性好,实时性差)

    opencv实现 详细原理:https://blog.csdn.net/u010440456/article/details/81483145

  2. Linux启动过程的C语言代码分析

    1. main函数 参见上方http://www.cnblogs.com/long123king/p/3543872.html,代码跳转到main函数. arch/x86/boot/main.c 1: ...

  3. 2019牛客多校第三场H-Magic Line

    Magic Line 题目传送门 解题思路 因为坐标的范围只有正负1000,且所有点坐标都是整数,所以所有点相连构成的最大斜率只有2000,而我们能够输出的的坐标范围是正负10^9.所以我们先把这n个 ...

  4. SDUTOJ 2498 数据结构实验之图论十一:AOE网上的关键路径

    题目链接:http://acm.sdut.edu.cn/onlinejudge2/index.php/Home/Index/problemdetail/pid/2498.html 题目大意 略. 分析 ...

  5. <python基础>python继承机制

    子类在调用某个方法或变量的时候,首先在自己内部查找,如果没有找到,则开始根据继承机制在父类里查找. 根据父类定义中的顺序,以深度优先的方式逐一查找父类! class D: def show(self) ...

  6. canvas 画一条折线

    设置画布对象 canvas id="myCanvas" ref="canvas" //获取Canvas对象(画布) var canvas = document. ...

  7. 如何在react中实现一个倒计时组件

    倒计时组件 import React, { Component } from 'react' import $ from 'jquery' import "../../css/spellTE ...

  8. 框架集 frameset

    框架集和内联框架的作用类似,都用于在一个页面中引入其他的外部的页面 框架集可以同时引入多个页面,而内联框架引入一个, 在h5标准中,推荐使用框架集,而不使用内联框架 使用 frameset 来创建一个 ...

  9. who - 显示已经登录的用户

    总览 (SYNOPSIS) who [OPTION]... [ FILE | ARG1 ARG2 ] 描述 (DESCRIPTION) -H, --heading 显示 栏目行 -i, -u, --i ...

  10. 从零开始搭建系统1.7——FTP安装及配置

    1.安装vsftp软件包 [root@localhost usr]# yum install -y vsftpd 2.先备份vsftpd的默认配置文件 [root@localhost usr]# cd ...