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

                                                                                         ------江财小子

  • 相关阅读:
    CSS
    回归分析过程实例(练习)
    结构方程模型处理二阶混合型(反映性+形成性)构念的方法
    python 列表推导式
    python3的enumerate函数
    SecureCRT上传下载文件
    scrapy连接MongoDB
    scrapy连接MySQL
    在Pycharm中运行Scrapy爬虫项目的基本操作
    mysql基础操作学习笔记(2)----索引
  • 原文地址:https://www.cnblogs.com/372465774y/p/2421695.html
Copyright © 2020-2023  润新知