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. LeetCode第[69]题(Java):Sqrt(x)

    题目:求平方根 难度:Easy 题目内容: Compute and return the square root of x, where x is guaranteed to be a non-neg ...

  2. python递归列出目录及其子目录下所有文件

    python递归列出目录及其子目录下所有文件 一.前言 函数的递归,简单来说,就是函数内部调用自己 先举个小例子,求阶乘 def factorial(n): if n == 0: return 1 e ...

  3. css 中的background:transparent到底是什么意思有什么作用

    有时我在看css时,看到有的css属性定义为background:transparent.意思就是背景透明.实际上background默认的颜色就是透明的属性.所以写和不写都是一样的 有段时间没写文章 ...

  4. Redis 应用笔记

    模糊匹配 语法:KEYS pattern 说明:返回与指定模式相匹配的所用的keys. 该命令所支持的匹配模式如下: (1)?:用于匹配单个字符.例如,h?llo可以匹配hello.hallo和hxl ...

  5. Webpack——解决疑惑,让你明白

    Webpack——解决疑惑,让你明白 极客教程 作者 关注 2016.09.29 17:46* 字数 2868 阅读 22204评论 22喜欢 80 Webpack是目前基于React和Redux开发 ...

  6. Ceph:pg peering过程分析

    转自:https://www.ustack.com/blog/ceph%ef%bc%8dpg-peering/ Peering:互为副本的三个(此处为设置的副本个数,通常设置为3)pg的元数据达到一致 ...

  7. 命令行连接db2数据库

    在cmd界面执行db2cmd命令 然后在db2cmd界面执行db2命令 然后执行 CONNECT TO UIBS USER DB2INST1 USING 123456命令

  8. 31-THREE.JS 正方体

    <!DOCTYPE html> <html> <head> <title>Example 05.04 - Basic 2D geometries - C ...

  9. FFT快速傅立叶变换

    //最近突然发现博客园支持\(\rm\LaTeX\),非常高兴啊! 话说离省选只有不到五天了还在学新东西确实有点逗…… 切到正题,FFT还是非常神奇的一个东西,能够反直觉地把两个多项式相乘的时间复杂度 ...

  10. Database项目中关于Procedure sp_refreshsqlmodule_internal的错误

    最近项目中发现一怪问题,使用DB项目发布数据库时,总提示 “(110,1): SQL72014: .Net SqlClient Data Provider: Msg 1222, Level 16, S ...