The solution of wikipedia is the elegant answer to this question. It finally passed on LeetCode after I modified and submited six times.
Sometimes we already have a binary tree, and we need to determine whether it is a BST. This problem has a simple recursive solution.
Essentially we keep creating a valid range (starting from [MIN_VALUE, MAX_VALUE]) and keep shrinking it down for each node as we go down recursively.
Problem
Given a binary tree, determine if it is a valid binary search tree (BST).
Java
1 | /** |
Submission Detail
- 75 / 75 test cases passed.
- Runtime: 0 ms
- Your runtime beats 100.00 % of java submissions.