We’re preparing your current view and syncing the latest data.
Given the root node of a binary search tree (BST) and a value val, you need to find the node in the BST where the node's value equals val and return the subtree rooted with that node. If such node does not exist, return null. A binary search tree is a binary tree in which for each node, all nodes in the left subtree have values less than the node's value, and all nodes in the right subtree have values greater than the node's value.
The input is the root node of a BST and an integer val representing the value to search for.
Return the root node of the subtree corresponding to the node with the value val, or null if no such node is found.
The number of nodes in the tree is in the range [1, 5000]. 1 <= Node.val <= 10^7. root is guaranteed to be a valid binary search tree.
Example 1
Input
root = [4,2,7,1,3], val = 2
Output
[2,1,3]
Explanation
The node with value 2 exists and its subtree contains nodes with values 2, 1 and 3.
Example 2
Input
root = [4,2,7,1,3], val = 5
Output
null
Explanation
There is no node with value 5 in this BST.