hdu3999 二叉排序树

The order of a Tree

Problem Description
As we know,the shape of a binary search tree is greatly related to the order of keys we insert. To be precisely:
1.  insert a key k to a empty tree, then the tree become a tree with
only one node;
2.  insert a key k to a nonempty tree, if k is less than the root ,insert
it to the left sub-tree;else insert k to the right sub-tree.
We call the order of keys we insert “the order of a tree”,your task is,given a oder of a tree, find the order of a tree with the least lexicographic order that generate the same tree.Two trees are the same if and only if they have the same shape.
 
Input
There are multiple test cases in an input file. The first line of each testcase is an integer n(n <= 100,000),represent the number of nodes.The second line has n intergers,k1 to kn,represent the order of a tree.To make if more simple, k1 to kn is a sequence of 1 to n.
 
Output
One line with n intergers, which are the order of a tree that generate the same tree with the least lexicographic.
 
Sample Input
4
1 3 4 2
 
Sample Output
1 3 2 4
 
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int pre[N], in[N], post[N];  //先序、中序、后序
int k;
struct node{
    int value;
    node *l, *r;
    //node(int value = 0, node *l = NULL, node *r = NULL):value(value), l(l), r(r){}
};
//void buildtree(int l, int r, int &t, node* &root) { //建树
//    int flag = -1;
//    for(int i = l; i <= r; i++) //先序的第一个是根,找到对应的中序的位置
//        if(in[i] == pre[t]){
//            flag = i; break;
//        }
//    if(flag == -1) return;       //结束
//    root = new node(in[flag]);   //新建结点
//    t++;
//    if(flag > l)  buildtree(l, flag - 1, t, root ->l);
//    if(flag < r)  buildtree(flag + 1, r, t, root ->r);
//}
void add(node * &root ,int & t ){
	if(root == NULL){
		root = new node; 
		root->value=t;
		root->l=root->r=NULL;
	}else{
		if( t > root->value) 
			add( root->r,t);
		else 
			add(root->l,t);
	}
	
}
void preorder (node *root){       //求先序序列
    if(root != NULL){
        post[k++] = root ->value;  //输出
        preorder (root ->l);
        preorder (root ->r);
    }
}
void inorder (node *root){        //求中序序列
    if(root != NULL){
        inorder (root ->l);
        post[k++] = root ->value;  //输出
        inorder (root ->r);
    }
}
void postorder (node *root){      //求后序序列
    if( root != NULL){
        postorder (root ->l);
        postorder (root ->r);
        post[k++] = root ->value;  //输出
    }
}
void remove_tree(node *root){      //释放空间
    if(root == NULL) return;
    remove_tree(root->l);
    remove_tree(root->r);
    delete root;
}
int main(){
    int n;   
	
    while(~scanf("%d", &n)){
		node *root=NULL;

		int x;
        for(int i=1;i<=n;i++){
        	 scanf("%d", &x);
        	 add(root,x );
        }
        k = 0;           //记录结点个数
        preorder(root);
        for(int i=0;i<k;i++) printf("%d%c",post[i], i==k-1? '\n' : ' ');
          //作为验证,这里可以用preorder()和inorder()检查先序和中序遍历
        remove_tree(root);
    }
    return 0;
}

  

 
 
 

转载于:https://www.cnblogs.com/lyj1/p/11518881.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值