hdu 3999 The order of a Tree (二叉搜索树)
/******************************************************************
题目: 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 (二叉搜索树)的更多相关文章
- HDU 3999 The order of a Tree 二叉搜索树 BST
建一个二叉搜索树,然后前序输出. 用链表建的,发现很久没做都快忘了... #include <cstdio> #include <cstdlib> struct Node{ i ...
- <hdu - 3999> The order of a Tree 水题 之 二叉搜索的数的先序输出
这里是杭电hdu上的链接:http://acm.hdu.edu.cn/showproblem.php?pid=3999 Problem Description: As we know,the sha ...
- 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 ...
- PAT甲级——1099 Build A Binary Search Tree (二叉搜索树)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90701125 1099 Build A Binary Searc ...
- [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 ...
- 235 Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最近公共祖先
给定一棵二叉搜索树, 找到该树中两个指定节点的最近公共祖先. 详见:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-s ...
- [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 ...
- [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 ...
- [LeetCode]501. Find Mode in Binary Search Tree二叉搜索树寻找众数
这次是二叉搜索树的遍历 感觉只要和二叉搜索树的题目,都要用到一个重要性质: 中序遍历二叉搜索树的结果是一个递增序列: 而且要注意,在递归遍历树的时候,有些参数如果是要随递归不断更新(也就是如果递归返回 ...
随机推荐
- [zz] be similar with和be similar to的区别
http://wenda.tianya.cn/question/4cb13da080ee34c9 be similar to后边既可以加物主代词又可以加人,即:be similar to sth/sb ...
- LeetCode "Binary Tree Level Order Traversal II" using DFS
BFS solution is intuitive - here I will show a DFS based solution: /** * Definition for a binary tre ...
- 原生js 实现购物车价格和总价 统计
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- javascript 使用btoa和atob来进行Base64转码和解码
javascript原生的api本来就支持,Base64,但是由于之前的javascript局限性,导致Base64基本中看不中用.当前html5标准正式化之际,Base64将有较大的转型空间,对于H ...
- JavaScript 数据验证类
JavaScript 数据验证类 /* JavaScript:验证类 author:杨波 date:20160323 1.用户名验证 2.密码验证 3.重复密码验证 4.邮箱验证 5.手机号验证 6. ...
- css学习笔记(4)
让顶部导航固定于页面的最顶端,无论页面上下滚动,顶部导航始终处在最顶端. *{ margin:0; padding:0}body{ padding-top:60px; }#nav{ width:100 ...
- XE6移动开发环境搭建之IOS篇(4):VMware9里安装Mac OSX 10.8(有图有真相)
网上能找到的关于Delphi XE系列的移动开发环境的相关文章甚少,本文尽量以详细的图文内容.傻瓜式的表达来告诉你想要的答案. 原创作品,请尊重作者劳动成果,转载请注明出处!!! 以下内容比较长,我们 ...
- flex polygon 序列化为txt 文本
当我们要把一个地块导出为txt的时候,应该怎么写,这是比较有用的这样可以帮助我们存档之类的,这里是基于某个地方的独立坐标系,是基于自己发布地图,如果是用百度地图或者其他网上的地图可能不适用. pack ...
- NHibernate系列文章十五:NHibernate组件
摘要 前面文章介绍了NHibernate对简单.net数据类型的映射对照表.NHibernate也可以映射复杂数据类型,这里介绍通过组件映射NHibernate值对象. 1. NHibernate引用 ...
- Git 版本控制 在 WIN 下的一些使用方法
这里记录一些 Git 在 Windows 操作系统下使用方法: 安装完毕后,先让Git 记录自己的名字: $ git config --global user.name "Your Name ...