问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4102 访问。

给定二叉搜索树(BST)的根节点和一个值。 你需要在BST中找到节点值等于给定值的节点。 返回以该节点为根的子树。 如果节点不存在,则返回 NULL。

给定二叉搜索树:

4

       / \

      2   7

     / \

    1   3

和值: 2

你应该返回如下子树:

2     

     / \   

    1   3

在上述示例中,如果要找的值是 5,但因为没有节点值为 5,我们应该返回 NULL。


Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST that the node's value equals the given value. Return the subtree rooted with that node. If such node doesn't exist, you should return NULL.

Given the tree:

        4

       / \

      2   7

     / \

    1   3

And the value to search: 2

You should return this subtree:

2     

     / \   

    1   3

In the example above, if we want to search the value 5, since there is no node with value 5, we should return NULL.


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4102 访问。

public class Program {

    public static void Main(string[] args) {
var root = new TreeNode(1) {
left = new TreeNode(3) {
left = new TreeNode(5),
right = new TreeNode(7)
},
right = new TreeNode(9)
}; var res = SearchBST(root, 5);
ShowTree(res);
Console.WriteLine(); res = SearchBST2(root, 6);
ShowTree(res); Console.ReadKey();
} public static void ShowTree(TreeNode node) {
if(node == null) {
Console.Write("null ");
return;
}
Console.Write($"{node.val} ");
ShowTree(node.left);
ShowTree(node.right);
} public static TreeNode SearchBST(TreeNode root, int val) {
if(root == null) return null;
if(root.val == val) return root;
var left = SearchBST(root?.left, val);
if(left != null) return left;
var right = SearchBST(root?.right, val);
if(right != null) return right;
return null;
} public static TreeNode SearchBST2(TreeNode root, int val) {
if(root == null) return null;
if(val > root.val) return SearchBST2(root.right, val);
if(val < root.val) return SearchBST2(root.left, val);
return root;
} public class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int x) { val = x; }
} }

以上给出2种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4102 访问。

5 null null
null

分析:

显而易见,以上2种算法的时间复杂度均为:  。

C#LeetCode刷题之#700-二叉搜索树中的搜索(Search in a Binary Search Tree)的更多相关文章

  1. LeetCode刷题预备知识(二)

    Python四大数据结构的属性及方法 在LeetCode刷题预备知识一中我们掌握了常见的内置函数,和四大数据结构的基本概念: 但只掌握这些还远远不够,我们还需了解四大数据结构的属性及方法才能更高效快速 ...

  2. [Swift]LeetCode501. 二叉搜索树中的众数 | Find Mode in Binary Search Tree

    Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred ...

  3. 【LeetCode 235_二叉搜索树】Lowest Common Ancestor of a Binary Search Tree

    解法一:递归 TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if (root == NULL | ...

  4. C#LeetCode刷题-二叉搜索树

    二叉搜索树篇 # 题名 刷题 通过率 难度 220 存在重复元素 III   19.3% 中等 315 计算右侧小于当前元素的个数   31.9% 困难 327 区间和的个数   29.5% 困难 3 ...

  5. LeetCode:二叉搜索树中的搜索【700】

    LeetCode:二叉搜索树中的搜索[700] 题目描述 给定二叉搜索树(BST)的根节点和一个值. 你需要在BST中找到节点值等于给定值的节点. 返回以该节点为根的子树. 如果节点不存在,则返回 N ...

  6. Java实现 LeetCode 700 二叉搜索树中的搜索(遍历树)

    700. 二叉搜索树中的搜索 给定二叉搜索树(BST)的根节点和一个值. 你需要在BST中找到节点值等于给定值的节点. 返回以该节点为根的子树. 如果节点不存在,则返回 NULL. 例如, 给定二叉搜 ...

  7. C#LeetCode刷题-树

    树篇 # 题名 刷题 通过率 难度 94 二叉树的中序遍历   61.6% 中等 95 不同的二叉搜索树 II   43.4% 中等 96 不同的二叉搜索树   51.6% 中等 98 验证二叉搜索树 ...

  8. LeetCode刷题总结-树篇(中)

    本篇接着<LeetCode刷题总结-树篇(上)>,讲解有关树的类型相关考点的习题,本期共收录17道题,1道简单题,10道中等题,6道困难题. 在LeetCode题库中,考察到的不同种类的树 ...

  9. LeetCode刷题总结-树篇(上)

          引子:刷题的过程可能是枯燥的,但程序员们的日常确不乏趣味.分享一则LeetCode上名为<打家劫舍 |||>题目的评论: 如有兴趣可以从此题为起点,去LeetCode开启刷题之 ...

  10. C#LeetCode刷题-设计

    设计篇 # 题名 刷题 通过率 难度 146 LRU缓存机制   33.1% 困难 155 最小栈 C#LeetCode刷题之#155-最小栈(Min Stack) 44.9% 简单 173 二叉搜索 ...

随机推荐

  1. wordpress学习笔记

    版本:4.9.8 我用wordpress的初衷是借用它的后台系统,前端用自己的网页显示存在wordpress数据库里的文章. wordpress本质上是个框架,技术栈:web-php-mysql. 初 ...

  2. java代码之美(17) ---Java8 LocalDateTime

    Java8 LocalDateTime 在java8之前我们在处理时间的时候都是用的Date,但它其实有很明显的缺点. 1.我们也会对日期做一些操作,比如加几天.加几分,当月的最后一天等等.有些计算实 ...

  3. 题解 洛谷 P6351 【[PA2011]Hard Choice】

    删边操作不好处理,所以先将操作倒序,将删边转化为加边. 考虑对于两个点的询问,若这两点不连通或这两个点分别处于两个不同的边双连通分量中(两点间存在桥)时,是不满足题目要求的. 可以用\(LCT\)来维 ...

  4. Linux字符集的查看及修改[转]

    一·查看字符集字符集在系统中体现形式是一个环境变量,以CentOS6.5为例,其查看当前终端使用字符集的方式可以有以下几种方式: 1.[root@david ~]# echo $LANGzh_CN.G ...

  5. MySQL之外键、联合查询、子查询

    外键(foreign key): 外面的键(键不在自己表中),如果一张表中有一个字段(非主键)指向另外一张表的主键,那么将该字段称之为外键. 外键可以在创建表的时候或者创建表之后增加(但是要考虑数据的 ...

  6. c++ string类型举例(递归预习的边界)

    #include <iostream> #include <string> using namespace std; int main ( ) { string str; // ...

  7. 17条嵌入式C语言编程小知识总结

    流水线被指令填满时才能发挥最大效能,即每时钟周期完成一条指令的执行(仅指单周期指令). 如果程序发生跳转,流水线会被清空,这将需要几个时钟才能使流水线再次填满.因此,尽量少的使用跳转指令可以提高程序执 ...

  8. mongoose.model第三个参数的问题

    这个是个好问题,之前按照教程做的数据库没有问题,现在自己从新做出现了问题.还好之前有无意中接触了这个知识点,不然感觉真的很难解决. 在检查完所有东西都没错的时候(前端传给req的数据正常,与数据库相符 ...

  9. Python File next() 方法

    概述 next() 方法在文件使用迭代器时会使用到,在循环中,next()方法会在每次循环中调用,该方法返回文件的下一行,如果到达结尾(EOF),则触发 StopIteration高佣联盟 www.c ...

  10. Python程序设计pdf|网盘下载内附提取码

    点击此处下载提取码:5o7z 本书提出了以理解和运用计算生态为目标的Python语言教学思想,不仅系统讲解了Python语言语法,同时介绍了从数据理解到图像处理的14个Python函数库,向初学Pyt ...