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. 基于web的jfreechart的使用

    这个模块的主要步骤就是: 前台通过struts调用后台,通过JFreeChart产生图片格式的图表,存储在某个位置,然后前台jsp再去调用图片. 来开工. JFreeChart的简介大家请百度. 首先 ...

  2. SpriteBuilder中如何给精灵添加帧动画

    首先你必须准备若干幅图片,当然最好做成Smart Sprite Sheet. 打开一个CCB文件,并鼠标选择根节点的CCSprite对象. 保持前者选中且Timeline的当前时间点把手在最左边,然后 ...

  3. rabbitMQ之AMQP协议

    1.什么是AMQP协议 即高级消息队列协议,规范客户端与消息中间件服务器之间的通信,并能相互操作. 2.AMQP协议的作用 降低应用程序之间的耦合度,这样不同应用之间的集成的难度将变得更小,并开发出更 ...

  4. 集群通信组件tribes之应用程序处理入口

    Tribes为了更清晰更好地划分职责,它被设计成用IO层和应用层,IO层专心负责网络传输方面的逻辑处理,把接收到的数据往应用层传送,当然应用层发送的数据也是通过此IO层发送,数据传往应用层后必须要留一 ...

  5. Java-HttpSession

    //session给用户一种标志,让用户可以在不同页面以及网站中都有一个特殊的标记 public interface HttpSession { /** * Returns the time when ...

  6. MDX的实例讲解(排名前15的小例子)

    MDX语句的特点: 大小写不分.members等于Members;downloads等于Downloads 维度的统计量指定要选择准确.downloads等于[Downloads] []可以少,不能多 ...

  7. IOS空数据页面,网络加载失败以及重新登陆View的封装(不需要继承)

    一.问题 对于B2C和B2B项目的开发者,可能会有一个订单列表为空,或者其他收藏页面为空,用户token失效,判断用户要重新登陆,以及后台服务错误等提示.本篇课文,看完大约10分钟. 原本自己不想写空 ...

  8. jsp面试题

    1, JSP中有那些内置对象,以及作用? 共有9种基本内置组件: request 用户端请求,此请求会包含来自GET/POST请求的参数: response 网页传回用户端的回应: pageConte ...

  9. JAVA 综合面试题

    JAVA 综合面试题 2007-08-12 目录 TOC \o "1-3" \h \z \u Java面试题整理 9 Java面向对象 9 1. super()与this()的区别 ...

  10. OSGI介绍

    OSGI介绍 OSGI简介 OSGI (Open Service Gateway Initiative)联盟成立于1999 年,它是一个非盈利的国际组织,旨在建立一个开放的服务规范,为通过网络向设备提 ...