-
-
Save InterviewBytes/57b7d333cae42315c270373dd124cf22 to your computer and use it in GitHub Desktop.
Valid BST
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.interviewbytes.trees; | |
public class TreeNode { | |
int val; | |
TreeNode left; | |
TreeNode right; | |
TreeNode(int x) { | |
val = x; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.interviewbytes.trees; | |
public class ValidBST { | |
public boolean isValidBST(TreeNode root) { | |
return helper(root, Long.MIN_VALUE, Long.MAX_VALUE); | |
} | |
private boolean helper(TreeNode root, long min, long max) { | |
return root == null | |
|| root.val > min && root.val < max | |
&& helper(root.left, min, root.val) | |
&& helper(root.right, root.val, max); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment