1099. Build A Binary Search Tree (30)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

思路

1.1064的老办法,将节点值升序排序后就是搜索树的中序遍历序列。

2.从根节点开始按中序遍历构造整棵树。

3.层次遍历打印整棵树(用队列BFS)。

代码

#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
class Node
{
public:
int left;
int right;
int val;
};
vector<Node> bstnodes();
vector<int> nodevalue();
int index; void createBST(int root)
{
if(bstnodes[root].left != -)
createBST(bstnodes[root].left);
bstnodes[root].val = nodevalue[index++];
if(bstnodes[root].right != -)
createBST(bstnodes[root].right);
} int main()
{
int N;
while(cin >> N)
{
index = ;
for(int i = ;i < N;i++)
{
cin >> bstnodes[i].left >> bstnodes[i].right;
}
for(int i = ;i < N;i++)
{
cin >> nodevalue[i];
}
sort(nodevalue.begin(),nodevalue.begin()+N);
createBST(); //print
queue<int> q;
q.push();
while(!q.empty())
{
int temp = q.front();
q.pop();
if(temp != )
cout <<" ";
cout << bstnodes[temp].val;
if(bstnodes[temp].left != -)
q.push(bstnodes[temp].left);
if(bstnodes[temp].right != -)
q.push(bstnodes[temp].right);
}
cout << endl;
}
return ;
}

PAT1099:Build A Binary Search Tree的更多相关文章

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

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

  2. PAT-1099(Build A Binary Search Tree)

    题目见这里 分析:分四步进行 1)根据给定的结点情况建二叉树  2)对输入的键值排序(asending) 3)对二叉树中序遍历,同时对应赋key值 4)层次遍历(队列应用) 题目并不困难,但是我误入了 ...

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

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

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

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

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

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

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

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

  8. PAT_A1099#Build A Binary Search Tree

    Source: PAT A1099 Build A Binary Search Tree (30 分) Description: A Binary Search Tree (BST) is recur ...

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

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

随机推荐

  1. 【matlab编程】Matlab版扫雷

    我发现有些人平常闲着的时候会玩window自带的游戏,其中最常见的就是扫雷和纸牌.本来想用matlab编写全自动扫雷程序用来作弊,可是后来发现扫雷问题是NP完全问题(正如:旅行商NP难问题一样不能被解 ...

  2. HTTP2概述

    HTTP/2 提供了HTTP语义的传输优化.HTTP/2支持所有HTTP/1.1的核心特征,并且在其他方面做的更高效. HTTP/2中基本的协议单位是帧.每个帧都有不同的类型和用途.例如,报头(HEA ...

  3. 64位ubuntu14.04配置adb后提示没有那个文件或目录

    1.配置完adb环境变量后在终端输入adb: ameyume@ameyume-HP-450-Notebook-PC:~$ adb /home/ameyume/adt-bundle-linux-x86_ ...

  4. Java进阶(十八)Java实现定时器(Timer)

    Java实现定时器(Timer) 绪 在应用开发中,经常需要一些周期性的操作,比如每5分钟执行某一操作等.对于这样的操作最方便.高效的实现方式就是使用java.util.Timer工具类.java.u ...

  5. SlopOne 改进

    lope One 其基本的想法来自于简单的一元线性模型 $w = f(v) = v + b$.已知一组训练点 ${(v_i, w_i)}_{i=1}^n$,利用此线性模型最小化预测误差的平方和,我们可 ...

  6. Erlang cowboy websocket 服务器

    Erlang cowboy websocket 服务器 原文见于: http://marcelog.github.io/articles/erlang_websocket_server_cowboy_ ...

  7. Android下Json数据解析

    如从网络获取JSON 则需要创建一个工具类,该类返回一个字符串为JSON文本 package com.example.jsonapp; import java.io.InputStreamReader ...

  8. 拆轮子之Fish动画分析

    概述 最近发现一个很好玩的动画库,纯代码实现的而不是通过图片叠加唬人的,觉得很有意思看了下源码https://github.com/dinuscxj/LoadingDrawable, 这个动画效果使用 ...

  9. Smali语法汇总(一)

    Opcode 操作码(hex) Opcode name 操作码名称 Explanation 说明 Example 示例 00 nop 无操作 0000 - nop 01 move vx, vy 移动v ...

  10. Linux内核中断和异常分析(中)

    在linux内核中,每一个能够发出中断请求的硬件设备控制器都有一条名为IRQ的输出线.所有现在存在的IRQ线都与一个名为可编程中断控制器的硬件电路的输入引脚相连,上次讲到单片机的时候,我就讲到了单片机 ...