Skip to content

Commit abf9a39

Browse files
committed
feat: validate-binary-search-tree
1 parent cf65e7b commit abf9a39

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/validate-binary-search-tree/">week02-5.validate-binary-search-tree</a>
3+
* <li>Description: determine if it is a valid binary search tree (BST). </li>
4+
* <li>Topics: Tree, Depth-First Search, Binary Search Tree, Binary Tree </li>
5+
* <li>Time Complexity: O(N), Runtime 0ms </li>
6+
* <li>Space Complexity: O(N), Memory 43.25MB </li>
7+
*/
8+
class Solution {
9+
public boolean isValidBST(TreeNode root) {
10+
return isValid(Integer.MIN_VALUE, Integer.MAX_VALUE, root);
11+
}
12+
13+
public boolean isValid(long min, long max, TreeNode node) {
14+
if(node == null) {
15+
return true;
16+
}
17+
18+
if (node.val < min || node.val > max) {
19+
return false;
20+
}
21+
22+
return isValid(min, (long) node.val - 1, node.left)
23+
&& isValid((long) node.val + 1, max, node.right);
24+
}
25+
26+
}
27+
28+

0 commit comments

Comments
 (0)