/******************************************************************
题目: The order of a Tree(hdu 3999)
链接: http://acm.hdu.edu.cn/showproblem.php?pid=3999
题意: 给你一个序列建立一棵二叉搜索树 要你找出另外一个序
列,可以建立和原序列建立的二叉搜索树一样且这个序列
是字典序最小
算法: 二叉搜索树
思想: 对于一个二叉搜索树,它的先序遍历是能建立该二叉搜索
树字典序最小序列
******************************************************************/
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
using namespace std; typedef struct Tree
{
Tree *left,*right;
int num;
}Tree;
Tree *t; Tree *inser(Tree *p,int x)
{
if (p==NULL)
{
p=(Tree *) malloc(sizeof(Tree));
p->left=p->right=NULL;
p->num=x;
return p;
}
if (p->num>x)
{
p->left=inser(p->left,x);
return p;
}
else
{
p->right=inser(p->right,x);
return p;
}
} void Find(Tree *p,int flag)
{
if (p==NULL) return ;
if (flag) printf("%d",p->num);
else printf(" %d",p->num);
Find(p->left,);
Find(p->right,);
delete(p);
} int main()
{
int n;
while (~scanf("%d",&n))
{
t=NULL;
for (int i=;i<n;i++)
{
int a;
scanf("%d",&a);
t=inser(t,a);
}
Find(t,);
printf("\n");
}
}

hdu 3999 The order of a Tree (二叉搜索树)的更多相关文章

  1. HDU 3999 The order of a Tree 二叉搜索树 BST

    建一个二叉搜索树,然后前序输出. 用链表建的,发现很久没做都快忘了... #include <cstdio> #include <cstdlib> struct Node{ i ...

  2. <hdu - 3999> The order of a Tree 水题 之 二叉搜索的数的先序输出

    这里是杭电hdu上的链接:http://acm.hdu.edu.cn/showproblem.php?pid=3999  Problem Description: As we know,the sha ...

  3. HDU 3999 The order of a Tree (先序遍历)

    The order of a Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  4. PAT甲级——1099 Build A Binary Search Tree (二叉搜索树)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90701125 1099 Build A Binary Searc ...

  5. [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  6. 235 Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最近公共祖先

    给定一棵二叉搜索树, 找到该树中两个指定节点的最近公共祖先. 详见:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-s ...

  7. [LeetCode] 235. Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  8. [LeetCode] 235. Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最近公共祖先

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  9. [LeetCode]501. Find Mode in Binary Search Tree二叉搜索树寻找众数

    这次是二叉搜索树的遍历 感觉只要和二叉搜索树的题目,都要用到一个重要性质: 中序遍历二叉搜索树的结果是一个递增序列: 而且要注意,在递归遍历树的时候,有些参数如果是要随递归不断更新(也就是如果递归返回 ...

随机推荐

  1. MySQL物理文件组成

    日志文件 错误日志:Error Log 错误日志记录了MySQL运行过程中所有较为严重的警告和错误信息,以及MySQL Server每次启动和关闭的详细信息.在默认情况下,系统记录错误日志的功能是关闭 ...

  2. linux删除某个php程序进程的组合命令

    如,想要杀死正在运行的所有  main.php 进程: ps aux |grep main.php|grep -v grep|awk '{print $2}'|xargs kill -9 解析: ps ...

  3. bootstrap-进度条

    <div class="container"> <div class="row"> <div class="col-lg ...

  4. 115、定时器(TimerTask+Timer+Handler)

    public class TimerUtils { public static Activity act; public static List<MaiDianModels> listMa ...

  5. ModelAttribute注解

    1.使用@ModelAttribute标记方法,会在每个目标方法执行前被springMVC调用 2.使用@ModelAttribute修饰目标方法pojo入参,其value属性值有以下作用: 1)sp ...

  6. 全面了解 Linux 服务器 - 2. 查看 Linux 服务器的内存使用情况

    2. 查看 Linux 服务器的内存使用情况 liuqian@ubuntu:~$ free -m total used free shared buffers cached Mem: 1983 186 ...

  7. ios开发 iphone中获取网卡地址和ip地址

    这是获取网卡的硬件地址的代码,如果无法编译通过,记得把下面的这几个头文件加上把. #include <sys/socket.h> // Per msqr#include <sys/s ...

  8. HDU5878

    http://acm.hdu.edu.cn/showproblem.php?pid=5878 给出你一个数字,让你求出大于这个数字n并且是形如2^a*3^b*5^c*7^d的最小的数: 就是用打表法求 ...

  9. Dynamics Webservice Call with Credential

    Dynamics Webservice call with credential /// <summary> ///WebServiceHelper 的摘要说明 /// </summ ...

  10. mac java目录

    /Library/Java/JavaVirtualMachines/jdk1.7.0_10.jdk/Contents/Home mac java的安装目录为 /Library/Java/JavaVir ...