Skip to content

Commit

Permalink
Time: 0 ms (100.00%), Space: 42.7 MB (99.14%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
kasmeen123 committed Nov 22, 2024
1 parent fea999f commit 23d1fa2
Showing 1 changed file with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isValidBST(TreeNode root) {
return check(root, Long.MIN_VALUE, Long.MAX_VALUE);
}
public boolean check(TreeNode node, long min, long max) {
if(node == null) return true;
if (node.val <= min || node.val >= max) {
return false;
}
return check(node.left, min, node.val) &&
check(node.right, node.val, max);
}
}

0 comments on commit 23d1fa2

Please sign in to comment.