建一个二叉搜索树,然后前序输出。

用链表建的,发现很久没做都快忘了。。。

#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的更多相关文章

  1. hdu 3999 The order of a Tree (二叉搜索树)

    /****************************************************************** 题目: The order of a Tree(hdu 3999 ...

  2. [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 ...

  3. C++版 - 剑指offer 面试题24:二叉搜索树BST的后序遍历序列(的判断) 题解

    剑指offer 面试题24:二叉搜索树的后序遍历序列(的判断) 题目:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则返回true.否则返回false.假设输入的数组的任意两个 ...

  4. 萌新笔记之二叉搜索树(BST)

    前言,以前搞过线段树,二叉树觉得也就那样= =.然后数据结构的课也没怎么听过,然后下周期中考... 本来以为今天英语考完可以好好搞ACM了,然后这个数据结构期中考感觉会丢人,还是好好学习一波. 二叉搜 ...

  5. 给定一个二叉搜索树(BST),找到树中第 K 小的节点

    问题:给定一个二叉搜索树(BST),找到树中第 K 小的节点. 出题人:阿里巴巴出题专家:文景/阿里云 CDN 资深技术专家. 考察点: 1. 基础数据结构的理解和编码能力 2.  递归使用 参考答案 ...

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

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

  7. 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 ...

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

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

  9. [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 ...

随机推荐

  1. 现在,UICollectionViews有了简单的重排功能

    原文:UICollectionViews Now Have Easy Reordering 我是UICollectionView的忠实粉丝.这个类比起它的老哥UITableView类具有更高的可定制性 ...

  2. hive UDF函数

    —虽然Hive提供了很多函数,但是有些还是难以满足我们的需求.因此Hive提供了自定义函数开发 —自定义函数包括三种UDF.UADF.UDTF —UDF(User-Defined-Function) ...

  3. POJ 2253 Frogger (dijkstra 最大边最小)

    Til the Cows Come Home 题目链接: http://acm.hust.edu.cn/vjudge/contest/66569#problem/A Description The i ...

  4. Spring @PostConstruct and @PreDestroy example

    In Spring, you can either implements InitializingBean and DisposableBean interface or specify the in ...

  5. Spring入门(2)-通过构造器注入Bean

    Spring入门(2)-通过构造器注入Bean 前一篇文章将了最基本的spring例子,这篇文章中,介绍一下带有参数的构造函数和通过构造器注入对象引用. 0. 目录 带有参数的构造函数 通过构造器注入 ...

  6. VC中监测函数运行时间

    VC++编程时,经常会监控某个算法的计算时间,以确定算法的效率.编码举例如下, //========start: algorithm time============= // SYSTEMTIME s ...

  7. 本地搜索神器-Everything

    现在硬盘越来越大了,经常机器上一堆资料,要找的时候,无论是XP还是Win7,都要搜索半天. 如果使用Everything,可以大大的加快这个过程. 具体的评价请看http://www.appinn.c ...

  8. SSH登录失败:Host key verification failed

    转载自:https://help.aliyun.com/knowledge_detail/41471.html 注意:本文相关 Linux 配置及说明已在 CentOS 6.5 64 位操作系统中进行 ...

  9. Oracle RMAN 清除归档日志

    在开发环境及UAT环境经常碰到需要清除归档日志的情形,对于这个问题方法有很多.可以直接使用rm方式清除归档日志,也可以使用find命令来查找符合条件的记录来清除归档日志,或者直接写个shell脚本来搞 ...

  10. PostgreSQL建表动作分析

    首先,建立表: pgsql=# create table tab10(id integer); CREATE TABLE pgsql::regclass; regclass ---------- ta ...