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

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

#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. TCPSocket v1.0 for cocos2d-x下载

    下载地址:http://files.cnblogs.com/elephant-x/TCPSocketLibs_V1.0.rar 这是自己封装的一个TCPSOCKET包,是独立于cocos2d-x的,使 ...

  2. NetAdvantage 笔记

    1.UltraControlBase Class Members 1.BeginUpdate Method Sets the IsUpdating flag to true which prevent ...

  3. hive UDF函数

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

  4. Gym 100818I Olympic Parade(位运算)

    Olympic Parade http://acm.hust.edu.cn/vjudge/contest/view.action?cid=101594#problem/I [题意]: 给出N个数,找出 ...

  5. HDU 2296 Ring (AC自动机+DP)

    Ring Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  6. AutoCAD.NET二次开发:扩展数据之XData

    结果缓存——ResultBuffer 结果缓存即 Autodesk.AutoCAD.DatabaseServices.ResultBuffer 类型,使用 ResultBuffer 对象时需要提供一个 ...

  7. jeecms支持的freemaker标签大全

    <@e.form id="jvForm" action="o_add.do"> <@e.text label="字段名" ...

  8. JAVA小笔记

    一.system.err.println() 和 system.out.println() 有什么不同 同时使用了System.out.println与System.err.println()打印输入 ...

  9. Objc基础学习记录1

    1.'-'系在实例方法前头 2.'+'类方法class method 相反; 3.void表示没有返回值; 4.&x 和c语言一样,代表的是x的在内存上的地址; 5.*y指向内存存储空间内的数 ...

  10. 如何在Android中使用OpenCV

    如何在Android中使用OpenCV 2011-09-21 10:22:35 标签:Android 移动开发 JNI OpenCV NDK 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始 ...