# Definition for a binary tree node.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def searchBST(self, root: TreeNode, val: int) -> TreeNode:
while root:
if val == root.val:
return root
elif val < root.val:
root = root.left
else:
root = root.right
return None
// Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
class Solution {
public:
TreeNode* searchBST(TreeNode* root, int val) {
while (root) {
if (val == root->val) return root;
else if (val < root->val) root = root->left;
else root = root->right;
}
return nullptr;
}
};
// 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) {
while (root != null) {
if (val == root.val) return root;
else if (val < root.val) root = root.left;
else root = root.right;
}
return null;
}
}
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @param {number} val
* @return {TreeNode}
*/
var searchBST = function(root, val) {
while (root) {
if (val === root.val) return root;
else if (val < root.val) root = root.left;
else root = root.right;
}
return null;
};
Given the root
of a binary search tree (BST) and an integer val
, you are to search for a node in the BST whose value equals val
. If such a node exists, return the subtree rooted at that node. If the node does not exist, return null
(or None
).
null
.To solve this problem, we first consider how a binary search tree (BST) works. In a BST, for any node, all nodes in the left subtree have smaller values, and all nodes in the right subtree have larger values. This property allows us to efficiently search for a value, rather than checking every node one by one (which would be required in a regular binary tree).
The naive approach would be to traverse every node (using DFS or BFS) and compare each node's value with val
. However, this ignores the BST property and would result in O(n) time complexity, where n is the number of nodes.
Instead, we can use the BST property to prune our search. At each node, we compare val
with the current node's value:
val
is equal, we've found the node and can return it.val
is less, we know the node must be in the left subtree.val
is greater, the node must be in the right subtree.
We will use an iterative approach (though recursion works equally well for this problem) to search for val
in the BST:
root
node.null
:
val
equals the current node's value, return the current node (this includes its entire subtree).val
is less than the current node's value, move to the left child.val
is greater than the current node's value, move to the right child.null
node, the value is not in the BST, so return null
.We use an iterative loop to avoid the overhead of recursion, but either approach is valid. This solution efficiently leverages the BST property, which is the key optimization.
Let's say our BST looks like this:
4 / \ 2 7 / \ 1 3
Suppose val = 2
.
If val = 5
:
By taking advantage of the BST property, we can efficiently search for a value without traversing the entire tree. At each step, we decide whether to go left or right based on a simple comparison, similar to binary search. This makes the algorithm much faster than a brute-force approach. The solution is both concise and efficient, making it an elegant way to search in a BST.