您的位置:首页 > 产品设计 > UI/UE

1099. Build A Binary Search Tree (30)

2016-03-10 14:20 429 查看


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

题目已经给出了二叉树的结构,主要的工作是将给出的数据依次分配到二叉树中的对应节点,然后可以按照广度优先搜索的方式来进行输出。考虑到将二叉树进行中序遍历后,恰好能得到其key值按照升序排列,因此可以先将给出的数值按照升序排列,然后通过中序遍历,将这个数值分配到二叉树中的各个对应节点。主要步骤可以分为三步:升序排列、中序遍历,将键值归位、广度优先搜索。

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <queue>
using namespace std;
void bfs();
void iner_transvasal(int id);
vector<int> num;
vector<int> out;
map<int,vector<int> > relation;
queue<int> iner;
int main()
{
    int n;
    cin >>n;
    for(int i=0;i<n;i++)
    {
        int tmp1,tmp2;
        cin >>tmp1 >>tmp2;
        relation[i].push_back(tmp1);
        relation[i].push_back(tmp2);
    }
    for(int i=0;i<n;i++)
    {
        int tmp;
        cin >>tmp;
        num.push_back(tmp);
    }
    //得到中序遍历的结果队列,便于后面进行中序遍历的时候,将每个数值与分配到每个相应的节点
    sort(num.begin(),num.end());
    vector<int>::iterator iter=num.begin();
    for(;iter!=num.end();iter++)
        iner.push(*iter);
    //中序遍历
    iner_transvasal(0);
    //广度优先搜索,将最后的结果存入到out向量中
    bfs();
    //输出结果
    iter=out.begin();
    cout <<*iter;
    iter++;
    for(;iter!=out.end();iter++)
        cout <<" " <<*iter;
    cout <<endl;
    return 0;
}
void bfs()
{
    queue<int> q;
    q.push(0);
    while(!q.empty())
    {
        int tmp,tmp1;
        tmp=q.front();
        tmp1=relation[tmp].at(2);
        out.push_back(tmp1);
        q.pop();
        vector<int> child;
        child=relation[tmp];
        if(child.at(0)!=-1)
            q.push(child.at(0));
        if(child.at(1)!=-1)
            q.push(child.at(1));
    }
}
void iner_transvasal(int id)
{
    if(relation[id].at(0)!=-1)
    {
        int tmp;
        tmp=relation[id].at(0);
        iner_transvasal(tmp);
    }
    int tmp;
    tmp=iner.front();
    iner.pop();
    relation[id].push_back(tmp);
    if(relation[id].at(1)!=-1)
    {
        int tmp;
        tmp=relation[id].at(1);
        iner_transvasal(tmp);
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: