hdu3999 binary sort tree

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.> 
the using namespace STD; 
const int N = 100010; 
int pre [N], in [N], POST [N]; // first order, in sequence, after 
int K; 
struct Node { 
    int value; 
    Node L *, * R & lt; 
    // Node (int value = 0, L * = NULL Node, R & lt Node * = NULL): value (value), L (L), R & lt (R & lt) {} 
} ; 
// void BuildTree (int L, R & lt int, int & T, the root Node * &) {// contribution 
// int In Flag = -1; 
// for (int I = L; I <= R & lt; I ++) // first the first sequence is the root, in order to find the corresponding position of the 
// IF (in [I] == pre [T]) { 
// in Flag = I; BREAK; 
//} 
// IF (in Flag == - 1) return; // end 
// root = new node (in [ flag]); // new node  
// 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 (the root -> R & lt); 
    } 
} 
void postorder (Node * the root) {// find the sequence after the sequence 
    IF (the root = NULL!) { 
        postorder (the root -> L); 
        postorder (the root -> R & lt); 
        POST [ k ++] = root -> value ; // output 
    } 
} 
void remove_tree (the root Node *) {// release space 
    IF (the root == NULL) return; 
    remove_tree (directory root-> L); 
    remove_tree (directory root-> R & lt); 
    Delete the root; 
} 
int main () { 
    int n-;    
	
    the while (~ Scanf ( "% D", & n-)) { 
		Node * the root = NULL; 

		int X; 
        for (int I =. 1; I <= n-; I ++) { 
        } 
        K = 0; // record the number of node
        	 scanf("%d", &x);
        	 the Add (the root, X); 
        preorder (the root); 
        for (int I = 0; I <K; I ++)? the printf ( "% D% C", POST [I], I == K-. 1 '\ n-' : ''); 
          // as authentication, it can be used herein preorder () and inOrder () checks the first sequence preorder and 
        remove_tree (the root); 
    } 
    return 0; 
}

  

 
 
 

Guess you like

Origin www.cnblogs.com/lyj1/p/11518881.html