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,
You should return this subtree:
In the example above, if we want to search the value5
, since there is no node with value5
, we should returnNULL
.
Solution
Get the value of a node and compare it with value we want to find. If value is less or equal then go to left if value is greater go right. And repeat this for all the nodes.
Last updated