zoukankan      html  css  js  c++  java
  • HDU_3999_二叉排序树

    The order of a Tree

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 1749    Accepted Submission(s): 908


    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
     
    给一个序列,依照序列建二叉排序树,输出建成这棵树的最小字典序列。
    不能使用线段树那种表示方式来表示树,因为可能会退化为一条链,这时会溢出。
    所以用lson和rson两个数组分别存每个结点的左右儿子(题中规定,数字为1—n)。
     
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<queue>
    using namespace std;
    
    int lson[100005],rson[100005],n;
    
    void Insert(int rt,int x)
    {
        if(rt>x)
        {
            if(lson[rt]==0)
                lson[rt]=x;
            else
                Insert(lson[rt],x);
        }
        else
        {
            if(rson[rt]==0)
                rson[rt]=x;
            else
                Insert(rson[rt],x);
        }
    
    }
    
    int cnt=0;
    void order(int rt)   //深搜
    {
        if(rt==0)
            return;
        printf("%d",rt);
        cnt++;
        if(cnt==n)
            printf("
    ");
        else
            printf(" ");
        order(lson[rt]);
        order(rson[rt]);
    }
    
    int main()
    {
        while(scanf("%d",&n)!=EOF)
        {
            cnt=0;
            int num,root;
            memset(lson,0,sizeof(lson));
            memset(rson,0,sizeof(rson));
            scanf("%d",&root);
            for(int i=1; i<n; i++)
            {
                scanf("%d",&num);
                Insert(root,num);
            }
            order(root);
            //cout<<n<<" "<<cnt<<endl;
        }
        return 0;
    }
     
  • 相关阅读:
    安装paramiko
    pip安装
    json
    java的枚举2
    java学习笔记1
    Myeclipse配置tomcat,以及简单的Myeclipse的配置
    Net分布式系统之四:RabbitMQ消息队列应用
    Net分布式系统之三:Keepalived+LVS+Nginx负载均衡之高可用
    Net分布式系统之二:CentOS系统搭建Nginx负载均衡(下)
    Spring简介
  • 原文地址:https://www.cnblogs.com/jasonlixuetao/p/5705150.html
Copyright © 2011-2022 走看看