Next Larger Value in a BST
You are given the root of a binary tree. Return the smallest value in the BST that is strictly greater than x, or None if there is no such value. x does not have to be in the tree. In a binary search tree every value in a left subtree is smaller than its node and every value in a right subtree is larger.
Trees appear in the tests in level order, with null for a missing child, like [3,9,20,null,null,15,7]. Each node is a TreeNode with .val, .left and .right; the class is already defined for you.
Example 1
root [5,3,8,2,4,7,9]
x 5
returns 7
The values above 5 are 7, 8, 9. The smallest is 7.
Constraints
0 <= number of nodes <= 100-1000 <= node.val <= 1000- The tree is a valid binary search tree.
-1000 <= x <= 1000
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.