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.

For example,

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.

Note that an empty tree is represented by NULL, therefore you would see the expected output (serialized tree format) as [], not null.

这道题让我们搜索一个二叉搜索树,既然是二叉搜索树,而不是普通的二叉树,那么我们肯定要利用二叉搜索树特定的性质来解题,即左<根<右。那么就是说任意一个结点的左子树中的所有结点均小于当前结点,其右子树中的所有结点均大于当前结点。那么这不就是一个天然的二分么,当仁不让的二分搜索法呼之欲出啊。首先判空,如果当前结点不存在,直接返回空。如果当前结点值等于目标值,返回当前结点。接下来就看如果当前结点值大于目标值,则对左子结点调用递归函数,否则就对右子结点调用递归函数,参见代码如下:

解法一:

class Solution {
public:
TreeNode* searchBST(TreeNode* root, int val) {
if (!root) return NULL;
if (root->val == val) return root;
return (root->val > val) ? searchBST(root->left, val) : searchBST(root->right, val);
}
};

我们也可以使用迭代形式来解,使用一个while循环,思路都是一样的,如果当前结点存在,且结点值不等于目标值,那么若结点值大于目标值,则当前结点指向其左子结点,否则指向其右子结点,参见代码如下:

解法二:

class Solution {
public:
TreeNode* searchBST(TreeNode* root, int val) {
while (root && root->val != val) {
root = (root->val > val) ? root->left : root->right;
}
return root;
}
};

类似题目:

Closest Binary Search Tree Value

Insert into a Binary Search Tree

参考资料:

https://leetcode.com/problems/search-in-a-binary-search-tree/

https://leetcode.com/problems/search-in-a-binary-search-tree/discuss/139687/Concise-iterative-solution-(C%2B%2B)

https://leetcode.com/problems/search-in-a-binary-search-tree/discuss/149274/Java-beats-100-concise-method-using-recursion-and-iteration

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Search in a Binary Search Tree 二叉搜索树中搜索的更多相关文章

  1. [LeetCode] Insert into a Binary Search Tree 二叉搜索树中插入结点

    Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert t ...

  2. 【LeetCode】98. Validate Binary Search Tree (2 solutions)

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  3. 【LeetCode】99. Recover Binary Search Tree 解题报告(Python)

    [LeetCode]99. Recover Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/p ...

  4. 【一天一道LeetCode】#96. Unique Binary Search Trees

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given n ...

  5. pat04-树7. Search in a Binary Search Tree (25)

    04-树7. Search in a Binary Search Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 ...

  6. 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)

    [LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...

  7. 04-树7. Search in a Binary Search Tree (25)

    04-树7. Search in a Binary Search Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 ...

  8. 【Leetcode_easy】700. Search in a Binary Search Tree

    problem 700. Search in a Binary Search Tree 参考1. Leetcode_easy_700. Search in a Binary Search Tree; ...

  9. [Algorithms] Refactor a Linear Search into a Binary Search with JavaScript

    Binary search is an algorithm that accepts a sorted list and returns a search element from the list. ...

  10. LeetCode初级算法--树02:验证二叉搜索树

    LeetCode初级算法--树02:验证二叉搜索树 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.ne ...

随机推荐

  1. NOI-OJ 1.13 ID:5 素数回文数的个数

    整体思路 使用埃拉拖色尼算法提前计算出1000以内的素数表. 设计一个回文判断函数isHW(int n),难点在于数字回文的判断.一个简单的方法是将数字n使用sprintf输出在一个数组中,然后从数组 ...

  2. Oracle使用PLSQL导入数据后中文乱码的解决方法

    新建环境变量 名:NLS_LANG 值:SIMPLIFIE DCHINESE_CHINA.ZHS16GBK 保存后重启PLSQL Developer 重新导入. 如果还是乱码,将上面8的环境变量值改为 ...

  3. django分页器

    网站页面的分页效果可以通过分页器实现 分页器的使用 urls.py from django.contrib import admin from django.urls import path from ...

  4. [再寄小读者之数学篇](2014-06-21 Beal-Kaot-Majda type logarithmic Sobolev inequality)

    For $f\in H^s(\bbR^3)$ with $s>\cfrac{3}{2}$, we have $$\bex \sen{f}_{L^\infty}\leq C\sex{1+\sen{ ...

  5. IDEA 代码规范插件

    前言 在工作过程中,每个人的代码习惯都不同,在一起工作做同一个项目,如果按照自己的习惯来,有可能造成代码维护困难,开发进度缓慢等. 代码规范的重要性 谷歌发布的代码规范中指出,80% 的缺失是由 20 ...

  6. struts2简单入门-OGNL表达式

    什么是OGNL表达式 Object-Graph Navigation Language的缩写. 可以遍历整个对象结构图,实现对象类型转换等功能的表达式. OGNL实际上是个Map集合,有一个上下文根对 ...

  7. ES6走一波 数组的扩展

    Array flat 数组实例的扁平化方法(浏览器支持不佳) 建议使用 lodash的 flatten

  8. PERFECT NUMBER PROBLEM(思维)

     题目链接:https://nanti.jisuanke.com/t/38220 题目大意:这道题让我们判断给定数字是否为完美数字,并给来完美数字的定义,就是一个整数等于除其自身之外的所有的因子之和. ...

  9. underscore用法大全

    1._.find函数 var one = _.find(all, function (item) { return item.C_ID == selected; }); $('#C_NAME').va ...

  10. java控台输入

    import java.util.Scanner;//访问util包的Scanner(控台输入) public class HelloWorld {public static void main(St ...