1.题目给出一个数组,要求构成完全的BST。
2.建立一个n节点的 完全二叉树,然后使用中序遍历,把地址数组遍历出来,再填充数组。
3.注意读取的数组需要先排序,在填充到完全二叉树的中序遍历数组中。
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
AC代码:
[c language=”++”]
//#include<string>
//#include<stack>
//#include<unordered_set>
//#include <sstream>
//#include "func.h"
//#include <list>
#include <iomanip>
#include<unordered_map>
#include<set>
#include<queue>
#include<map>
#include<vector>
#include <algorithm>
#include<stdio.h>
#include<iostream>
#include<string>
#include<memory.h>
#include<limits.h>
#include<stack>
using namespace std;
struct TreeNode{
int val;
TreeNode*l, *r;
TreeNode() :val(-1), l(NULL), r(NULL){};
};
void inOrder(TreeNode*root, vector<TreeNode*>&ans)
{
if (root != NULL)
{
inOrder(root->l, ans);
ans.push_back(root);
inOrder(root->r, ans);
}
}
int main(void)
{
int n;
cin >> n;
if (n == 0)
{
cout << endl;
return 0;
}
vector<TreeNode> tree(n);
queue<TreeNode*> q;
int idx = 0;
q.push(&tree[idx++]);
int count1 = 1;
int count2 = 0;
//建立一棵节点数为n的完全二叉树
while (idx < n)
{
for (int i = 0; i < count1; i++)
{
TreeNode*head = q.front();
q.pop();
head->l = &tree[idx++];
q.push(head->l);
count2++;
if (idx == n)
break;
head->r = &tree[idx++];
q.push(head->r);
count2++;
if (idx == n)
break;
}
count1 = count2;
count2 = 0;
}
//把树按照中序遍历出一个数组,然后填充数值,主要读取的数组要先排序
vector<TreeNode*> in(0);
inOrder(&tree[0], in);
vector<int> num(n);
for (int i = 0; i < in.size(); i++)
{
scanf("%d", &num[i]);
}
sort(num.begin(), num.end());
for (int i = 0; i < in.size(); i++)
{
in[i]->val = num[i];
}
queue<TreeNode*> bfs;
bfs.push(&tree[0]);
count1 = 1;
count2 = 0;
vector<int> ans(0);
int idxOut = 0;
while (!bfs.empty())
{
for (int i = 0; i < count1; i++)
{
TreeNode*head = bfs.front();
bfs.pop();
if (idxOut == 0)
printf("%d", head->val);
else
printf(" %d", head->val);
idxOut++;
if (head->l != NULL)
{
bfs.push(head->l);
count2++;
}
if (head->r != NULL)
{
bfs.push(head->r);
count2++;
}
}
count1 = count2;
count2 = 0;
}
return 0;
}
[/c]