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 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的更多相关文章
- 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 作者 ... 
- 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 作者 ... 
- 【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; ... 
- [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. ... 
- 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 ... 
- 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 ... 
- [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 ... 
- [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 ... 
- 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 ... 
- LintCode题解之Search Range in Binary Search Tree
		1.题目描述 2.问题分析 首先将二叉查找树使用中序遍历的方式将元素放入一个vector,然后在vector 中截取符合条件的数字. 3.代码 /** * Definition of TreeNode ... 
随机推荐
- wifi androd 整体框架
			1. http://blog.csdn.net/myarrow/article/details/8129607/ 2. http://blog.csdn.net/liuhaomatou/articl ... 
- jQuery的事件绑定和解除
			1 . 绑定事件 语法 : bind(type,data,fn) 描述 : 为每一个匹配的特定元素(像 click)绑定一个事件处理器函数. type(String) : 事件类型 data(Obje ... 
- 可信执行环境(TEE)介绍 与应用
			原文:http://blog.csdn.net/wed110/article/details/53894927 可信执行环境(TEE,Trusted Execution Environment) 是G ... 
- Matlab的publish功能和cell功能
			Matlab的publish功能能够让写的代码变成优美的文档.类似为知笔记的markdown语言. cell功能配合publish使用,可以形成不同的功能块.而且调试的时候,可以按section调试. ... 
- db2move 数据导出整理
			db2move <database-name> <action> [<option> <value>] 命令解释:1).database-name, ... 
- tflearn 在每一个epoch完毕保存模型
			关键代码:tflearn.DNN(net, checkpoint_path='model_resnet_cifar10', max_checkpoints=10, tensorboard_verbos ... 
- 微信小程序module.exports 模块化
			//common.js var studentList = [ { name: "xiaoming", age: "22" ... 
- zepto.fullpage
			内容来自:颜海镜 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ... 
- 洛谷P1525关押罪犯——二分做法
			题目:https://www.luogu.org/problemnew/show/P1525 二分答案,二分图染色判断是否可行. 代码如下: #include<iostream> #inc ... 
- CentOS6.6中安装VNC server(CentOS配置远程桌面)
			1.安装服务 yum install tigervnc-server 1 2 名字有点怪哦,CentOS5前叫vnc-server 2.运行并设置密码 vncserver + 回车 1 2 输入密码, ... 
