File tree 2 files changed +18
-1
lines changed
2 files changed +18
-1
lines changed Original file line number Diff line number Diff line change 89
89
| 95 | [ Unique Binary Search Trees II] ( https://leetcode.com/problems/unique-binary-search-trees-ii ) | [ ![ Java] ( assets/java.png )] ( src/UniqueBinarySearchTreesII.java ) | |
90
90
| 96 | [ Unique Binary Search Trees] ( https://leetcode.com/problems/unique-binary-search-trees ) | [ ![ Java] ( assets/java.png )] ( src/UniqueBinarySearchTrees.java ) | |
91
91
| 97 | [ Interleaving String] ( https://leetcode.com/problems/interleaving-string ) | | |
92
- | 98 | [ Validate Binary Search Tree] ( https://leetcode.com/problems/validate-binary-search-tree ) | | |
92
+ | 98 | [ Validate Binary Search Tree] ( https://leetcode.com/problems/validate-binary-search-tree ) | [ ![ Java ] ( assets/java.png )] ( src/ValidateBinarySearchTree.java ) | |
93
93
| 99 | [ Recover Binary Search Tree] ( https://leetcode.com/problems/recover-binary-search-tree ) | | |
94
94
| 100 | [ Same Tree] ( https://leetcode.com/problems/same-tree ) | [ ![ Java] ( assets/java.png )] ( src/SameTree.java ) [ ![ Python] ( assets/python.png )] ( python/same_tree.py ) | |
95
95
| 101 | [ Symmetric Tree] ( https://leetcode.com/problems/symmetric-tree ) | [ ![ Java] ( assets/java.png )] ( src/SymmetricTree.java ) [ ![ Python] ( assets/python.png )] ( python/symmetric_tree.py ) | |
Original file line number Diff line number Diff line change
1
+ // https://leetcode.com/problems/validate-binary-search-tree
2
+ // T: O(n)
3
+ // S: O(log(n))
4
+
5
+ public class ValidateBinarySearchTree {
6
+ public boolean isValidBST (TreeNode root ) {
7
+ return isValidBST (root , Long .MIN_VALUE , Long .MAX_VALUE );
8
+ }
9
+
10
+ private boolean isValidBST (TreeNode root , long minLeftVal , long maxRightVal ) {
11
+ if (root == null ) return true ;
12
+ return minLeftVal < root .val
13
+ && root .val < maxRightVal
14
+ && isValidBST (root .left , minLeftVal , root .val )
15
+ && isValidBST (root .right , root .val , maxRightVal );
16
+ }
17
+ }
You can’t perform that action at this time.
0 commit comments