You are given the root of a Binary Search Tree (BST) and a target node. Your task is to find its inorder successor.
The inorder successor of a node is the node with the smallest value greater than the target's value.
If no such node exists, return null.
Input: root = [5,3,8,1,4,null,9], target = 4
Output: 5
Explanation: In inorder traversal (1, 3, 4, 5, 8, 9), the value that immediately follows 4 is 5.
Input: root = [5,3,8,1,4,null,9], target = 9
Output: null
Explanation: 9 is the largest value in the BST, so it has no inorder successor.
Input: root = [2,1,3], target = 1
Output: 2
Explanation: The inorder sequence is 1, 2, 3, so the successor of 1 is 2.
Approach, complexity and clean code in every language. Sign in to unlock the editorial.
Your submissions, results and runtime history live here. Sign in to start your streak.
50 30 70 20 40 60 80 40
50
40 20 60 10 30 50 70 30
40