zoukankan      html  css  js  c++  java
  • HDU3999: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 <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    typedef struct TREE
    {
        struct TREE *l,*r;
        int num;
    } tree;
    
    tree *creat(tree *t,int x)
    {
        if(t == NULL)
        {
            t = (tree*)malloc(sizeof(tree));
            t->num = x;
            t->l = t->r = NULL;
            return t;
        }
        if(t->num > x)
            t->l = creat(t->l,x);
        else
            t->r = creat(t->r,x);
        return t;
    }
    
    void libian(tree *t,int x)
    {
        if(x == 1)
            printf("%d",t->num);
        else
            printf(" %d",t->num);
        if(t->l!=NULL)
            libian(t->l,2);
        if(t->r!=NULL)
            libian(t->r,2);
    }
    
    int main()
    {
        int i,m,n;
        tree *t = NULL;
        while(~scanf("%d",&n))
        {
            for(i = 0;i<n;i++)
            {
                scanf("%d",&m);
                t = creat(t,m);
            }
            libian(t,1);
            printf("\n");
        }
        return 0;
    }
    


  • 相关阅读:
    公式编辑器mathtype中一些符号显示方框的解决方法
    I got my first job
    我的第二个面试通知
    清空visual studio2010的查找历史
    King Back
    IIS中“使用 XSL 样式表无法查看 XML 输入”问题的解决
    JDBC 各种连接方式[转载]
    力扣每日刷题(1)
    力扣每天刷题(3)
    力扣每天刷题(2)
  • 原文地址:https://www.cnblogs.com/xinyuyuanm/p/2999146.html
Copyright © 2011-2022 走看看