天天看点

PAT1099 Build A Binary Search Tree (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.

  • 题目大意

    给定一棵树的结构以及整数序列,求由这个整数序列和给定的树的结构构成的BST树,并输出这棵树的层次遍历的整数序列

  • 解题思路

    1.中序遍历对给定树的结构中树的节点进行赋值

    2.层次遍历输出节点的值

  • 代码实现
#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
const int MAX = 101;
struct Node{
    int left;
    int right;
    int data;
}node[MAX];
vector<int> num;
int k = 0;
void in_order(int root){           //中序遍历输入节点的值
    if(root == -1)
        return;
    else{
        in_order(node[root].left);
        node[root].data = num[k++];
        in_order(node[root].right);
    }
}
//层次遍历输出节点的值
vector<int> ans;
void level_order(int root){
    queue<int> qu;
    qu.push(root);           //根节点入队
    while(!qu.empty()){
         int temp = qu.front();
         qu.pop();             //出队
         ans.push_back(node[temp].data);   //存储数据信息
         if(node[temp].left != -1)
            qu.push(node[temp].left);
         if(node[temp].right != -1)
            qu.push(node[temp].right);
    }
}
int main()
{
    int N;
    scanf("%d", &N);
    for(int i = 0; i < N; i++){           //输入树的信息
        int left, right;
        scanf("%d%d", &left, &right);
        node[i].left = left;
        node[i].right = right;
    }
    for(int i = 0; i < N; i++){
        int temp1;
        scanf("%d", &temp1);
        num.push_back(temp1);
    }
    sort(num.begin(), num.end());
    in_order(0);                     //创建BST树
    level_order(0);                  //层次遍历BST树
    for(int i = 0; i < ans.size(); i++){
        if(i != 0)
            printf(" %d", ans[i]);
        else
            printf("%d", ans[0]);
    }
    return 0;
}

           

继续阅读