Data Structures19 sections · 729 units
Open in Course

Problem - Search in BST

Find a node by value

Given the root of a BST and an integer val, find the node with that value and return the subtree rooted at that node. If the node doesn't exist, return null.

Example: In BST [4,2,7,1,3][4,2,7,1,3], searching for 2 returns subtree [2,1,3][2,1,3]. You're implementing the most basic BST operation.

Use the BST property to decide which direction to search. Constraints: up to 50005000 nodes, values from 11 to 10710^7.