PAT 1064. Complete Binary Search Tree (30)(中序遍历来给完全搜索树赋值,题目是给出一个列数字,把它构建成完全搜索树并输出)

来源:互联网 发布:淘宝护盾 编辑:程序博客网 时间:2024/06/11 05:28

官网

1064. Complete 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.
A Complete Binary Tree (CBT) is a tree that is completely filled, with the possible exception of the bottom level, which is filled from left to right.

Now given a sequence of distinct non-negative integer keys, a unique BST can be constructed if it is required that the tree must also be a CBT. You are supposed to output the level order traversal sequence of this BST.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (<=1000). Then N distinct non-negative integer keys are given in the next line. All the numbers in a line are separated by a space and are no greater than 2000.

Output Specification:

For each test case, print in one line the level order traversal sequence of the corresponding complete binary search tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.

Sample Input:
10
1 2 3 4 5 6 7 8 9 0
Sample Output:
6 3 8 1 5 7 9 0 2 4

题目大意

  • 1.给出一个列数字,把它构建成完全搜索树并层次遍历输出。

解题思路

  • 1.因为是完全搜索树,所以给出树的节点的个数,这棵树长什么样子就确定了,然后把给出的这列数字从小到大排序,那么再用中序遍历遍历这棵树,并给这棵树从小到大赋值,我们可以发现,这个完全搜索树已经构建成功了。

AC代码

#include<iostream>#include<deque>#include<vector>#include<algorithm>using namespace std;int n,k=0;vector<int> keep;typedef struct node{    int value;    node * left;    node * right;    node():left(NULL),right(NULL){}    //node  (int v):value(v),left(NULL),right(NULL){}}* Tree;//依据这个树的大小n,把这棵树的框架搭起来Tree build(){    Tree root = new node;    k++;    deque<Tree> p;    p.push_back(root);    while (!p.empty()) {        Tree t = p.front();        if (k<n) {            t->left = new node;            p.push_back(t->left);            k++;        }else {            break;        }        if (k<n) {            t->right = new node;            p.push_back(t->right);            k++;        }else {            break;        }        p.pop_front();    }    return root;}//利用中序遍历给每个节点赋值void inOrder(Tree root){    if (root == NULL) {        return;    }    inOrder(root->left);    root->value = keep[k++];    inOrder(root->right);}//层次遍历输出bool flag = false;void print(Tree root){    deque<Tree> p;    p.push_back(root);    while (!p.empty()) {        Tree t = p.front();        if (!flag) {            cout << t->value;            flag= true;        }else {            cout << " " << t->value;        }        if (t->left != NULL) {            p.push_back(t->left);        }        if (t->right != NULL) {            p.push_back(t->right);        }        p.pop_front();    }}int main(int argc, char *argv[]){    cin >> n;    keep.resize(n);    for (int i = 0; i < n; ++i) {        cin >> keep[i];    }    //排序    sort(keep.begin(),keep.end());    Tree root = build();    k = 0;    inOrder(root);    print(root);    cout << endl;    return 0;}
0 0
原创粉丝点击