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. MySQL初步

    一 写在开头1.1 本节内容本节的主要内容是MySQL的基本操作(来自MySQL 5.7官方文档). 1.2 工具准备一台装好了mysql的ubuntu 16.04 LTS机器. 二 MySQL的连接 ...

  2. pymysql常见问题

    1.Python中pymysql出现乱码的解决方法 一般来说,在使用mysql最麻烦的问题在于乱码. 查看mysql的编码: show variables like 'character_set_%' ...

  3. Centos7安装vsftpd (FTP服务器)

    Centos7安装vsftpd (FTP服务器) 原文链接:https://www.jianshu.com/p/9abad055fff6 TyiMan 关注 2016.02.06 21:19* 字数 ...

  4. linux 只查看目录下文件夹

    只显示目录文件夹 ls -F |grep "/$" 显示 目录权限 ls -al |grep "^d" 只显示文件 ls -al |grep "^-& ...

  5. Notepad++ --v7.5.8 (64bit) 安装目录显示插件(Explorer)

    https://blog.csdn.net/qq_24153697/article/details/83036761 最近想自己做一个小项目,用Notepad做IDE,但是发现已安装的Notepad没 ...

  6. Permission denied的解决办法

    在运行TensorFlow Example的mnist_dataset_intro时出现了Permission denied的问题,这一看就是权限问题. 解决的办法: $ sudo chmod -R ...

  7. 使用Cobbler批量部署Linux和Windows:Windows系统批量安装(三)

    Tutorial: Installing Windows with cobbler (cobbler安装Windows) Windows系统的自动安装需要用到Win PE工具.流程如下: 定制Win ...

  8. (一)Java工程化--Maven基础

    Maven 读作['mevən] 翻译成中文是"内行,专家" Maven是什么 包依赖的前世今生: 原始的jar包引用--> ant --> maven. 是一种项目管 ...

  9. monkey测试 -- 原理和操作步骤

     Monkey测试原理: Monkey是Android中的一个命令行工具,可以运行在模拟器里或实际设备中.它向系统发送伪随机的用户事件流(如按键输入.触摸屏输入.手势输入等),实现对正在开发的应用程序 ...

  10. SQL Server - DISTINCT

    http://www.runoob.com/sql/sql-distinct.html 只选出不同的值,过滤掉重复的值.