1633. Range Keeper Node
A library shelves books by catalogue key in a binary search tree root; all keys are distinct, smaller keys on the left and larger on the right. A visitor asks for every book whose key is in the closed interval from low to high inclusive, and the librarian needs one starting point from which the whole interval can be reached.
Return, by reference, the node of the original tree that is closest to the root and whose key lies in [low, high]; this node is an ancestor-or-self of every in-range key. If no key lies in the interval, return null. Do not build a new node. Use the ordering of the tree to descend directly: O(h) time and O(1) extra space.
Example 1
- Input:
- root = [8,3,10,1,6,null,14,null,null,4,7,13]low = 4high = 7
- Output:
- 4
- Explanation:
The node at level-order index 4 is the shallowest key in range.
Example 2
- Input:
- root = [8,3,10,1,6,null,14,null,null,4,7,13]low = 15high = 20
- Output:
- null
- Explanation:
No key lies in the interval, so the answer is null.
Constraints
1 ≤ nodes ≤ 1000; keys distinct; -104 ≤ low ≤ high ≤ 104; -104 ≤ Node.val ≤ 104
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 1,200 msC++ 300 msJava 600 msJavaScript 600 msTypeScript 600 ms
Expected complexity
- Time
- O(h)
- Space
- O(1)