285. Inorder Successor in BST
Practice Inorder Successor in BST for free on NudgeCode: Medium, Binary Tree pattern. Write it in Python or JavaScript, run the tests in your browser, and ask for a hint when you are stuck.
Given the root of a binary search tree and a node p in it, return the in-order successor of p: the node with the smallest value greater than p.val. Return null if p has no successor.
The tests give p by its value (all values are distinct), and the expected output is the successor's value.
Example
root = [2,1,3], p = 1 -> 2
root = [5,3,6,2,4,null,null,1], p = 6 -> null
Constraints
- 1 <= number of nodes <= 10^4
- -10^5 <= Node.val <= 10^5, all values distinct
- Aim for O(h) time, where h is the height of the tree