1099 Build A Binary Search Tree (30)(30 分)
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
 
题意:
给出二叉树的结构和数字序列,把该序列填入二叉树中,使其该树成为一棵BST。输出层序序列。
 
思路:
1、根据给出的树的结构构建二叉树。(静态写法,root题目规定为0)
2、将输入的序列从小到大排序。
3、中序遍历二叉树,把序列中的元素逐个填入(利用BST中序序列是有序序列的性质)
4、层序遍历

代码:

#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
;
struct Node{
    int val;
    int left,right;
}Tree[N];
int data[N];
int n;

void inOrderTraversal(int root)
{
    ;
    ){
        inOrderTraversal(Tree[root].left);
        Tree[root].val=data[idx++];
        inOrderTraversal(Tree[root].right);
    }
}

void layerOrderTraversal(int root)
{
    ;
    queue<int> q;
    q.push(root);
    while(!q.empty()){
        int top=q.front();
        q.pop();
        printf("%d",Tree[top].val);
        idx++;
        if(idx<n) printf(" ");
        ) q.push(Tree[top].left);
        ) q.push(Tree[top].right);
    }
}

int main()
{
    scanf("%d",&n);
    int u,v;
    ;i<n;i++){
        scanf("%d%d",&u,&v);
        Tree[i].left=u;
        Tree[i].right=v;
    }
    ;i<n;i++)
        scanf("%d",&data[i]);
    sort(data,data+n);
    inOrderTraversal();//中序遍历,填入数据
    layerOrderTraversal();
    ;
}

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

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

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

  6. PAT 甲级 1099 Build A Binary Search Tree

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

  7. PAT 1099. Build A Binary Search Tree (树的中序,层序遍历)

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

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

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

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

随机推荐

  1. Java多线程 - Callable和Future

    已知的创建多线程的方法有继承Tread类和实现Runnable方法.此外Java还提供了Callable接口,Callable接口也提供了一个call()方法来做为线程执行体.但是call()方法与r ...

  2. 虚拟主机(多站点配置)的实现--centos上的实现

    Apache中配置多主机多站点,可以通过两种方式实现 将同一个域名的不同端口映射到不同的站点(虚拟主机) 将同一个端口映射成不同的域名,不同的域名映射到不同的站点 两种方法可以同时存在,局域网通过   ...

  3. [转]Python读写文件

    1.open使用open打开文件后一定要记得调用文件对象的close()方法.比如可以用try/finally语句来确保最后能关闭文件. file_object = open('thefile.txt ...

  4. CC国内厂商现状

    (1)阿里云的产品 CC攻击 攻击者攻击服务器的认证页面.登录页面.游戏论坛等.还是用饭馆的例子,CC攻击相当于,坏人霸占收银台结账.霸占服务员点菜,导致正常的客人无法享受到服务. 游戏盾如何防御CC ...

  5. 【专题】区间dp

    1.[nyoj737]石子合并 传送门:点击打开链接 描述    有N堆石子排成一排,每堆石子有一定的数量.现要将N堆石子并成为一堆.合并的过程只能每次将相邻的两堆石子堆成一堆,每次合并花费的代价为这 ...

  6. .zip.001 -- .zip.003解压缩

    一.源文件移到固定短路径 二.cmd执行合并 copy /B 201702.zip.001 + 201702.zip.002 + 201702.zip.003 1.zip 三.WinRAR解压缩

  7. spring容器和上下文的理解

    spring容器和上下文的理解 spring框架现在使用的很多,这说明有其独特之处----依赖注入,很简单的四个字,在我们需要对象的时候,spring就可以给我们提供所需要的对象,对象的创建.销毁.管 ...

  8. TCPL学习笔记:编写expand(s1, s2),将字符串s1中类似于a-z一类的速记符号在s2中扩充完整。可以处理大小写及字符,以及a-b-c, a-z0-9以及-a-z等多种情况。

    话不多说,看代码: #include <stdio.h> #include <stdlib.h> int main(void) { ] = "a-z0-9hahah- ...

  9. SpringCloud教程 | 第十一篇: docker部署spring cloud项目

    版权声明:本文为博主原创文章,欢迎转载,转载请注明作者.原文超链接 ,博主地址:http://blog.csdn.net/forezp. http://blog.csdn.net/forezp/art ...

  10. Java 面试/笔试题神整理 [Java web and android]

    Java 面试/笔试题神整理 一.Java web 相关基础知识 1.面向对象的特征有哪些方面 1.抽象: 抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象并 ...