HDU 3999 The order of a Tree 二叉搜索树 BST
建一个二叉搜索树,然后前序输出。
用链表建的,发现很久没做都快忘了。。。
- #include <cstdio>
- #include <cstdlib>
- struct Node{
- int v;
- Node* l;
- Node* r;
- };
- Node* root;
- Node* newnd(int value) {
- Node* u = (Node*) malloc(sizeof(Node));
- if (u != NULL) {
- u -> v = value;
- u -> r = u -> l = NULL;
- }
- return u;
- }
- Node* addnd(int value, Node* u) {
- if (u == NULL)
- return newnd(value);
- if (u -> v > value)
- u -> r = addnd(value, u -> r);
- else
- u -> l = addnd(value, u -> l);
- return u;
- }
- void input(Node* u, int n) {
- if (n == 0)
- printf("%d", u->v);
- else
- printf(" %d", u->v);
- if (u -> r != NULL)
- input(u -> r, n + 1);
- if (u -> l != NULL)
- input(u -> l, n + 1);
- }
- int main() {
- int n, tmp;
- scanf("%d", &n);
- root = NULL;
- for (int i = 0; i < n; i++){
- scanf("%d", &tmp);
- root = addnd(tmp, root);
- }
- input(root, 0);
- printf("\n");
- return 0;
- }
HDU 3999 The order of a Tree 二叉搜索树 BST的更多相关文章
- hdu 3999 The order of a Tree (二叉搜索树)
/****************************************************************** 题目: The order of a Tree(hdu 3999 ...
- [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 ...
- C++版 - 剑指offer 面试题24:二叉搜索树BST的后序遍历序列(的判断) 题解
剑指offer 面试题24:二叉搜索树的后序遍历序列(的判断) 题目:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则返回true.否则返回false.假设输入的数组的任意两个 ...
- 萌新笔记之二叉搜索树(BST)
前言,以前搞过线段树,二叉树觉得也就那样= =.然后数据结构的课也没怎么听过,然后下周期中考... 本来以为今天英语考完可以好好搞ACM了,然后这个数据结构期中考感觉会丢人,还是好好学习一波. 二叉搜 ...
- 给定一个二叉搜索树(BST),找到树中第 K 小的节点
问题:给定一个二叉搜索树(BST),找到树中第 K 小的节点. 出题人:阿里巴巴出题专家:文景/阿里云 CDN 资深技术专家. 考察点: 1. 基础数据结构的理解和编码能力 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 ...
- 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 ...
随机推荐
- 分布式应用框架Akka快速入门
转自:http://blog.csdn.net/jmppok/article/details/17264495 本文结合网上一些资料,对他们进行整理,摘选和翻译而成,对Akka进行简要的说明.引用资料 ...
- Hibernate检索策略
1. Hibernate的检索策略概述: 检索数据时的 2 个问题: 1.不浪费内存:当 Hibernate 从数据库中加载 Customer 对象时, 如果同时加载所有关联的 Order 对象 ...
- ASP.NET网站中设置404自定义错误页面
在用ASP.NET WebForm开发一个网站时,需要自定义404错误页面. 做法是这样的 在网站根目录下建立了一个404.html的错误页面,然后在Global.asax文件中,加入如下代码: &l ...
- ASP.NET项目中引用全局dll
在ASP.NET项目中,有些dll是全局dll,也就是说,没有放在单个项目的引用中.它们一般存放在如下目录C:\Windows\assembly中 这个时候,我们需要在单个项目中引用他们,应该如何做呢 ...
- Unity3D之Mecanim动画系统学习笔记(六):使用脚本控制动画
控制人物动画播放 这里我重新弄了一个简单的场景和新的Animator Controller来作为示例. 下面先看看Animator Controller的配置: 人物在站立状态只能进入走路,走路只能进 ...
- cocos2d-x Mask的实现及优化
转自:http://blog.ch-wind.com/cocos2d-x%E4%B8%ADmask%E7%9A%84%E5%AE%9E%E7%8E%B0%E5%8F%8A%E4%BC%98%E5%8C ...
- UVa657 The die is cast
// 题意:给一个图案,其中'.'表示背景,非'.'字符组成的连通块为筛子.每个筛子里又包含两种字符,其中'X'组成的连通块表示筛子上的点 // 统计每个筛子里有多少个"X"连通块 ...
- PostgreSQL中,如何查表属于哪个数据库
db1=# \x Expanded display is on. db1=# SELECT * FROM information_schema.tables WHERE table_name='tab ...
- SRV记录说明
SRV记录是DNS服务器的数据库中支持的一种资源记录的类型,它记录了哪台计算机提供了哪个服务这么一个简单的信息 SRV 记录:一般是为Microsoft的活动目录设置时的应用.DNS可以独立于活动 ...
- Codeforces Round #332 (Div. 2) B. Spongebob and Joke 水题
B. Spongebob and Joke Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/599 ...