Skip to content

Instantly share code, notes, and snippets.

@InterviewBytes
Created June 11, 2017 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save InterviewBytes/57b7d333cae42315c270373dd124cf22 to your computer and use it in GitHub Desktop.
Save InterviewBytes/57b7d333cae42315c270373dd124cf22 to your computer and use it in GitHub Desktop.
Valid BST
package com.interviewbytes.trees;
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
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