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. Cocos2d-X开发中国象棋《九》走棋规则

    在上一节中实现了走棋,这篇博客将介绍中国象棋中的走棋规则 在写博客前先可能一下象棋的走棋规则: 1)将 将的坐标关系:横坐标相等,纵坐标相减绝对值等于1,或者纵坐标相等,横坐标相减绝对值等于1 将的特 ...

  2. 24、Cocos2dx 3.0游戏开发找小三之网格动作:高炫酷的3D动作

    重开发人员的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/37596763 网格动作类似于动作特效,能够实现翻转. ...

  3. ubuntu中设置wireshark抓包

    安装wireshark软件后,打开进行抓包的时候会提示权限不足.原因是普通用户没有执行权限,也打不开网络端口捕捉,因为dumpcap需要root权限. 产生这种问题的原因:比如:wireshark在进 ...

  4. (转)CentOS 5.5 64bit 编译安装Adobe FlashMediaServer 3.5

    http://download.macromedia.com/pub/flashmediaserver/updates/4_0_2/Linux_32bit/FlashMediaServer4.tar. ...

  5. 使用python转换编码格式

    之前有写过一个使用powershell转换文档格式的方法,然而因为powershell支持不是很全,所以并不好用.这里使用python再做一个. 思路 检测源码格式,如果不是utf8,则进行转换,否则 ...

  6. 话说文件系统——aufs源码分析(三)【转】

    本文转载自:http://www.cnblogs.com/xie0812/p/8848185.html 1. linux中设备驱动的入口都是:module_init(xxx_init);里面注册的函数 ...

  7. Keepalived实现心跳检测实现自动重启

    项目中服务器如果发生宕机:1.故障转移 2.心跳检测 3.负载均衡 4.自动重启 心跳检测: 心跳检测脚本: 写入nginx_check.sh脚本 vi  /etc/keepalived/nginx_ ...

  8. logback备注

    <?xmlversion="1.0"encoding="UTF-8"?> <!-- <configuration>包含的属性 sc ...

  9. linux命令学习笔记(44):top命令

    top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管 理器.下面详细介绍它的使用方法.top是一个动态显示过程,即可以通过用户按键来不断刷 ...

  10. xxx referenced from: xxx in xxx.o

    情形一:可能是有一些源码文件没有加入工程所导致的,找到相应的.h和.m文件,将其add进入项目工程即可解决这种问题. 情形二:也有可能是某些framework没有加入项目中, 示例:   Undefi ...