• 1064 Complete Binary Search Tree


    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 (≤). 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

    题意:

      给出一组数字,用这组数字构成一棵完全二叉搜索树。

    思路:

      完全二叉搜索树中序遍历的结果,就是升序排序的结果。所以我们可以根据结点的个数,构造出一棵完全二叉搜索树的框架,然后再中序遍历这棵树,将所给的数组排序后依次插入即可。

    Code:  

     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 typedef struct Node* node;
     6 
     7 struct Node {
     8     int val;
     9     node left;
    10     node right;
    11     Node(int v) {
    12         val = v;
    13         left = NULL;
    14         right = NULL;
    15     }
    16 };
    17 
    18 void buildTree(node& root, int num) {
    19     queue<node> que;
    20     que.push(root);
    21     int count = 1;
    22     while (count < num) {
    23         node temp = que.front();
    24         que.pop();
    25         temp->left = new Node(-1);
    26         que.push(temp->left);
    27         count++;
    28         if (count < num) {
    29             temp->right = new Node(-1);
    30             que.push(temp->right);
    31             count++;
    32         }
    33     }
    34 };
    35 
    36 int tempIndex = 0;
    37 void inorderTraveral(node& root, vector<int>& keys) {
    38     if (root == NULL) return;
    39     inorderTraveral(root->left, keys);
    40     root->val = keys[tempIndex++];
    41     inorderTraveral(root->right, keys);
    42 }
    43 
    44 void levelTraveral(node& root) {
    45     queue<node> que;
    46     que.push(root);
    47     bool isFirst = true;
    48     while (!que.empty()) {
    49         node temp = que.front();
    50         que.pop();
    51         if (isFirst) {
    52             cout << temp->val;
    53             isFirst = false;
    54         } else {
    55             cout << " " << temp->val;
    56         }
    57         if (temp->left != NULL) que.push(temp->left);
    58         if (temp->right != NULL) que.push(temp->right);
    59     }
    60 }
    61 
    62 int main() {
    63     int n;
    64     cin >> n;
    65     vector<int> keys(n);
    66     for (int i = 0; i < n; ++i) cin >> keys[i];
    67     sort(keys.begin(), keys.end());
    68     node root = new Node(-1);
    69     buildTree(root, n);
    70     inorderTraveral(root, keys);
    71     levelTraveral(root);
    72     return 0;
    73 }
    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    学习 Ext 中一些不错的编码习惯
    actionbar tab字体颜色
    Android Library工程实现模块复用(代码及资源文件)
    Fragment之底部导航栏的实现
    actionbar修改背景颜色
    Android在桌面添加可拖动、点击的悬浮窗口
    请问GridView中 两个图片之间的间距是如何设置的
    http://www.55zm.com/a/20120702/38037.html
    android开源系列:CircleImageView自定义圆形控件的使用
    App版本更新时对SQLite数据库升级或者降级遇到的问题
  • 原文地址:https://www.cnblogs.com/h-hkai/p/12829908.html
Copyright © 2020-2023  润新知