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 ...
随机推荐
- php总结5——常量、文件上传
5.1常量 系统常量: PHP_OS 操作系统 PHP_VERSION php版本 PHP_SAPI 运行方式 自定义常量: define("常量名称"," ...
- 微软Azure区块链开发工具包三大功能详解
2018年11月15日,微软宣布了Azure区块链开发工具包,它基于微软的无服务器技术构建,并且利用微软和第三方SaaS,完美集成了区块链.该工具包扩展了微软的区块链开发模板和Azure Blockc ...
- nvl()与regexp_replace()
NVL(字段,0):将空值转化为0 regexp_replace(字段, ‘[1-9]‘ ,'') is not null; 将数字转化为0
- ls bash: cannot create temp file for here-document: No space left on device
出现这种问题,一般是磁盘空间满了,或者是inode满了 使用命令: df -h查询磁盘空间 df -i 查询inode占用 Filesystem Inodes IUsed IFree IUse% Mo ...
- c#应用程序带参数运行
有时候我们需要让软件带参数运行,使用参数控制软件的部分行为, C#默认窗口应用是不带参数的,不过在Main函数的参数手动加上就可以得到参数了. 举例如下: /// <summary> // ...
- LightOJ - 1038 Race to 1 Again —— 期望
题目链接:https://vjudge.net/problem/LightOJ-1038 1038 - Race to 1 Again PDF (English) Statistics Foru ...
- visual studio for mac 安装文件
安装步骤参考: http://jingyan.baidu.com/article/00a07f3869b81082d028dca8.html 所需安装文件:[注 我只下载了javajdk, 需要and ...
- L90
On Motes and Beams 微尘与栋梁 It is curious that our own offenses should seem so much less heinous than t ...
- ECMAScript Obejct 属性操作API
Object 创建对象 我们有很多种方法创建一个对象,Object.create, Object.assign Object.create //字面量 var o = {}; // 等同于 var o ...
- codeforces 658C C. Bear and Forgotten Tree 3(tree+乱搞)
题目链接: C. Bear and Forgotten Tree 3 time limit per test 2 seconds memory limit per test 256 megabytes ...