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.

Approach #1: C++.

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* searchBST(TreeNode* root, int val) {
while (root != nullptr && root->val != val) {
root = (root->val > val) ? root->left : root->right;
}
return root;
}
};

  

Approach #2: Java.

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode searchBST(TreeNode root, int val) {
if (root == null || root.val == val) return root;
return root.val > val ? searchBST(root.left, val) : searchBST(root.right, val);
}
}

  

Approach #3: Python.

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def searchBST(self, root, val):
"""
:type root: TreeNode
:type val: int
:rtype: TreeNode
"""
if root and root.val > val:
return self.searchBST(root.left, val)
if root and root.val < val:
return self.searchBST(root.right, val)
return root

  

Search in a Binary Search Tree的更多相关文章

  1. 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 作者 ...

  2. 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 作者 ...

  3. 【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; ...

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

  5. Search Range in Binary Search Tree

    Given two values k1 and k2 (where k1 < k2) and a root pointer to a Binary Search Tree. Find all t ...

  6. Lintcode: Search Range in Binary Search Tree

    Given two values k1 and k2 (where k1 < k2) and a root pointer to a Binary Search Tree. Find all t ...

  7. [Swift]LeetCode700. 二叉搜索树中的搜索 | Search in a Binary Search Tree

    Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST ...

  8. [LeetCode] Search in a Binary Search Tree 二叉搜索树中搜索

    Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST ...

  9. LeetCode 700 Search in a Binary Search Tree 解题报告

    题目要求 Given the root node of a binary search tree (BST) and a value. You need to find the node in the ...

  10. LintCode题解之Search Range in Binary Search Tree

    1.题目描述 2.问题分析 首先将二叉查找树使用中序遍历的方式将元素放入一个vector,然后在vector 中截取符合条件的数字. 3.代码 /** * Definition of TreeNode ...

随机推荐

  1. while 循环中的break continue pass 的用法

    while break:跳出最近的循环 continue:跳到最近所在循环的开头处 pass:什么也不做,只是空占位语句,它本身与循环没什么关系,但属于简单的单个单词语句的范畴: pass 语句是无运 ...

  2. SAP FI 科目代码

    资产类 现金 银行存款 其他货币资金 短期投资 短期投资跌价准备 应收票据 应收股利 应收利息 应收账款 其他应收款 坏账准备 预付账款 应收补贴款 物料采购 原材料 包装物 低值易耗品 材料成本差异 ...

  3. Swift 烧脑体操(三) - 高阶函数

    前言 Swift 其实比 Objective-C 复杂很多,相对于出生于上世纪 80 年代的 Objective-C 来说,Swift 融入了大量新特性.这也使得我们学习掌握这门语言变得相对来说更加困 ...

  4. WebsiteCrawler

    看到网上不少py的爬虫功能极强大,可惜对py了解的不多,以前尝试过使用c# WebHttpRequert类来读取网站的html页面源码,然后通过正则表达式筛选出想要的结果,但现在的网站中,多数使用js ...

  5. LightOJ - 1287 Where to Run —— 期望、状压DP

    题目链接:https://vjudge.net/problem/LightOJ-1287 1287 - Where to Run    PDF (English) Statistics Forum T ...

  6. Spring Boot2.0之全局捕获异常

    全局捕获异常,很明显的错误404返回给客户,很不好呀.整个web请求项目全局捕获异常,比如空指针直接返回给客户啊,那多操蛋呀~ 看这几个常用的注解: @ExceptionHandler 表示拦截异常 ...

  7. Kafka0.7运行时报错 kafka/javaapi/consumer/ConsumerConnector : Unsupported major.minor version 51.0 解决

    目前中央库中 org.apache.kafka 是用jdk1.7编译的, 故跑在1.6的jvm中会报错 解决方案: 1. 下载kafka源码, 本地sbt进行install, 编译前 java -ve ...

  8. Mysql远程登陆错误:ERROR 2003

    不能远程登陆Mysql,错误:ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.0.114' (10060).原因是电脑防火墙 ...

  9. css书写规则

    无规矩不成方圆,不管有多少人共同参与同一项目,一定要确保每一行代码都像是同一个人编写的 不要在自闭合(self-closing)元素的尾部添加斜线 不要省略可选的结束标签(closing tag)(例 ...

  10. BZOJ_2111_[ZJOI2010]Perm 排列计数_树形DP+组合数学

    Description 称一个1,2,...,N的排列P1,P2...,Pn是Magic的,当且仅当2<=i<=N时,Pi>Pi/2. 计算1,2,...N的排列中有多少是Magic ...