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. extundelete实现Linux下文件/文件夹数据恢复!

    我用的是Centos系统,在安装extundelete之前需要安装e2fsprogs,e2fsprogs-libs,e2fsprogs-devel. 这里用:yum install e2fsprogs ...

  2. mvvm2

    1:设计模式 在MVP模式中,为了让UI层能够从逻辑层上分离下来,设计师们在UI层与逻辑层之间加了一层interface.无论是UI开发人员还是数据开发人员,都要尊重这个契约.按照它进行设计和开发.这 ...

  3. easyui datagrid 行编辑功能

    datagrid现在具有行编辑能力了,使用时只须在columns中为需要编辑的列添加一个editor属性,编辑保存时同时具有数据校验能力. 看一个例子效果图: 代码如下: $('#tt').datag ...

  4. Android ADT远程主机强迫关闭了一个现有的连接 Connection attempts: 1 解决方法

    adb有一个限制, 也可以说是bug.  当手机上同时运行的进程数大于64时, 就会引发adb奔溃. 更深层次的原因, 就是windows API的WaitForMultipleObjects所支持的 ...

  5. C++(二十二) — 指针变量、函数指针、void指针

    1.指针变量 (1)指针变量必须在初始化后才可以正常使用,初始化就是给他分配一个有效的数据地址. 先初始化,后使用. (2)指针可以进行加减运算,++ 或者 --:将指针的位置向前或者向后移动一个数据 ...

  6. 一般处理程序ashx中用session存储数据

    如果要使用session的话,在handler的代码中添加System.Web.SessionState的引用,并让这个handler继承IRequiresSessionState接口,一定要继承这个 ...

  7. Node——用http-proxy 做反向代理服务器

    时下不少场景,都是申请一个 VPS 主机来托管运行 Web 项目的,小弟我也不例外——购买了一个小型的 CentOS VPS 使用着.在使用的过程中,面临一个问题,就是同一类型的服务端环境还好——但如 ...

  8. Android性能调优实例

    本文主要分享自己在appstore项目中的性能调优点,包括同步改异步.缓存.Layout优化.数据库优化.算法优化.延迟执行等. 目前性能优化专题已完成以下部分: 性能优化总纲——性能问题及性能调优方 ...

  9. Agilent RF fundamentals (9)-Mixer basics

    Function: change the frequency of the incident RF key parameters ----------------------------------- ...

  10. 第一次使用stackoverflow的寻求帮助

    最近在研究一项资料很少的技术--Drools,遇到一个很棘手的问题,搜遍所有网站,百度.谷歌都找不到解决方案.无奈之下,想起了stackoverflow这个技术问答网站.于是鼓足勇气,用蹩脚的英文在上 ...