1064. Complete Binary Search Tree (30)

原创 2016年06月01日 22:21:54
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


IDEA

1.完全二叉排序树,设每个节点序号为 i属于[0~n-1],则其左孩子序号为2*i+1,右孩子为2*i+2

2.二次排序树进行中序遍历后,得到节点值排序是从小到大的序列


CODE

#include<iostream>
#include<vector> 
#include<queue>
#include<algorithm>
#include<fstream>
using namespace std;
#define MAX 1001
struct Node{
	int data;
	int left,right;
};
Node tree[MAX];
int data[MAX];
int n;
void InOrder(int root){
	static int index=0;
	if(root>=n){
		return;
	}
	InOrder(tree[root].left);
	tree[root].data=data[index++];
	//cout<<root<<" "<<tree[root].data<<endl;
	InOrder(tree[root].right);
}
void LevelOrder(int root,vector<int> &seq){
	queue<int> que;
	que.push(root);
	while(!que.empty()){
		int tmp=que.front();
		que.pop();
		seq.push_back(tree[tmp].data);
		if(tree[tmp].left<n){
			que.push(tree[tmp].left);
		}
		if(tree[tmp].right<n){
			que.push(tree[tmp].right);
		}
	}
}
int main(){
	#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	#endif
	
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>data[i];
	}
	sort(data,data+n);
	
	for(int i=0;i<n;i++){
		tree[i].left=2*i+1;
		tree[i].right=2*i+2;		
	}
	
	int root=0;
	InOrder(root);
	
	vector<int> seq;
	LevelOrder(root,seq);
	
	vector<int>::iterator it;
	for(it=seq.begin();it!=seq.end();it++){
		if(it==seq.begin()){
			cout<<*it;
		}else{
			cout<<" "<<*it;
		}
	}	
	#ifndef ONLINE_JUDGE
	fclose(stdin);
	#endif
	
	return 0;
}


版权声明:

相关文章推荐

1064. Complete Binary Search Tree (30)

1. 原题: https://www.patest.cn/contests/pat-a-practise/1064 2. 思路: 题意: 给出一个序列,构建出它的完全二叉搜索树, 然后输出层序。...

1064. Complete Binary Search Tree (30)

1.建议一个n节点的 完全二叉树,然后使用中序遍历,把地址数组遍历出来,再填充数组 2.注意读取的数组需要先排序,在填充到完全二叉树的中序遍历数组中 //#include //#inclu...

1064. Complete Binary Search Tree (30)

A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie...

1064. Complete Binary Search Tree (30)【二叉树】——PAT (Advanced Level) Practise

题目信息1064. Complete Binary Search Tree (30)时间限制100 ms 内存限制65536 kB 代码长度限制16000 BA Binary Search Tre...

PAT 1064. Complete Binary Search Tree (30)

1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度...

1064. Complete Binary Search Tree (30)

Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 ...

1064. Complete Binary Search Tree (30)

1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度...

1064. Complete Binary Search Tree (30)

题目链接:http://www.patest.cn/contests/pat-a-practise/1064 题目: A Binary Search Tree (BST) is recur...

1064. Complete Binary Search Tree (30)

A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie...

1064. Complete Binary Search Tree (30)

30分的水题,果然最重要的永远是idea
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)