zoukankan      html  css  js  c++  java
  • hdu 3999 The order of a Tree (二叉搜索树)

     1 /******************************************************************
     2 题目:        The order of a Tree(hdu 3999)
     3 链接:        http://acm.hdu.edu.cn/showproblem.php?pid=3999
     4 题意:        给你一个序列建立一棵二叉搜索树 要你找出另外一个序
     5               列,可以建立和原序列建立的二叉搜索树一样且这个序列
     6               是字典序最小
     7 算法:        二叉搜索树
     8 思想:        对于一个二叉搜索树,它的先序遍历是能建立该二叉搜索
     9               树字典序最小序列
    10 ******************************************************************/
    11 #include<cstdio>
    12 #include<cstring>
    13 #include<cstdlib>
    14 #include<iostream>
    15 using namespace std;
    16 
    17 typedef struct Tree
    18 {
    19     Tree *left,*right;
    20     int num;
    21 }Tree;
    22 Tree *t;
    23 
    24 Tree *inser(Tree *p,int x)
    25 {
    26     if (p==NULL)
    27     {
    28         p=(Tree *) malloc(sizeof(Tree));
    29         p->left=p->right=NULL;
    30         p->num=x;
    31         return p;
    32     }
    33     if (p->num>x)
    34     {
    35         p->left=inser(p->left,x);
    36         return p;
    37     }
    38     else
    39     {
    40         p->right=inser(p->right,x);
    41         return p;
    42     }
    43 }
    44 
    45 void Find(Tree *p,int flag)
    46 {
    47     if (p==NULL) return ;
    48     if (flag) printf("%d",p->num);
    49     else printf(" %d",p->num);
    50     Find(p->left,0);
    51     Find(p->right,0);
    52     delete(p);
    53 }
    54 
    55 int main()
    56 {
    57     int n;
    58     while (~scanf("%d",&n))
    59     {
    60         t=NULL;
    61         for (int i=0;i<n;i++)
    62         {
    63             int a;
    64             scanf("%d",&a);
    65             t=inser(t,a);
    66         }
    67         Find(t,1);
    68         printf("
    ");
    69     }
    70 }
  • 相关阅读:
    /dev/null
    useradd
    linux防火墙
    安装ntp服务同步服务器时间
    使用WTM框架项目的部署遇到的问题及解决方式
    .net5 winform 打开文件夹
    maven打包命令
    url.openconnection() 设置超时时间
    java判断http地址是否连通
    解决 curl: (35) Encountered end of file
  • 原文地址:https://www.cnblogs.com/pblr/p/5732705.html
Copyright © 2011-2022 走看看