• 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;
    }

                                                                                         ------江财小子

  • 相关阅读:
    Web框架下安全漏洞的测试反思
    如何能低成本地快速获取大量目标用户,而不是与竞争对手持久战?
    Spring-Boot自定义Starter实践
    WM_QUERYENDSESSION与WM_ENDSESSION
    AutoMapper用法
    使用AutoMapper实现Dto和Model的自由转换(下)
    使用AutoMapper实现Dto和Model的自由转换(中)
    使用AutoMapper实现Dto和Model的自由转换(上)
    JSON中JObject和JArray的修改
    通信对象 System.ServiceModel.Channels.ServiceChannel 无法用于通信,因为其处于“出错”状态。
  • 原文地址:https://www.cnblogs.com/372465774y/p/2421695.html
Copyright © 2020-2023  润新知