2.Search in a Binary Search Tree
Find the node in the BST that the node's value equals val
and return the subtree rooted with that node. If such a node does not exist, return null
.
You are given the root
of a binary search tree (BST) and an integer val
.
Example 1:
Example 2:
Solution Iteratively:-
Solution Recursively:-
Time Complexity of searching in a BST
Maximum number of comparisons is h
i.e height of binary search tree
So time complexity O(h) i.e O(log n).
Last updated