zoukankan      html  css  js  c++  java
  • HDU(3999)The order of a Tree

    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
    Source
    简单的二叉树题目,先建树,再前序遍历(开始以为中序呢,囧!)。

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    typedef struct
     node
    {

        int
     n;
        struct
     node *l,*r;
    }
    BT;
    BT* ct(BT *p,int n)//建树
    {

        if
    (p==NULL)
          {

             p=(BT*)malloc(sizeof(BT));
             p->n=n;
             p->l=p->r=NULL;
             return
     p;
          }

         if
    (p->n>n)
            p->l=ct(p->l,n);
           else

            p->r=ct(p->r,n);
        return
     p;
    }

    void
     ds(BT *p,int c)//前序遍历
    {

        if
    (c!=1)
           printf(" %d",p->n);
         else

           printf("%d",p->n);
         if
    (p->l!=NULL)
          ds(p->l,2);
        if
    (p->r!=NULL)
           ds(p->r,2);
    }

    int
     main()
    {

       int
     i,n,m;
      BT *p=NULL;
      while
    (scanf("%d",&n)!=EOF)
      {

           for
    (i=0;i<n;i++)
            {

                scanf("%d",&m);p=ct(p,m);
            }

          ds(p,1);printf("\n");
      }


      return
     0;
    }

                                                                                         ------江财小子

  • 相关阅读:
    将.lib库文件转换成.a库文件的工具
    协议
    协议
    bzoj1066
    bzoj2668
    bzoj2245
    bzoj2324后续思考
    bzoj2324
    jsoi2014前两轮回眸
    bzoj1293
  • 原文地址:https://www.cnblogs.com/372465774y/p/2421695.html
Copyright © 2011-2022 走看看