Skip to content

Instantly share code, notes, and snippets.

@cixuuz
Created August 27, 2017 21:11
Show Gist options
  • Save cixuuz/c7bea3857fcbf20cdc9e4e2040847b35 to your computer and use it in GitHub Desktop.
Save cixuuz/c7bea3857fcbf20cdc9e4e2040847b35 to your computer and use it in GitHub Desktop.
[98. Validate Binary Search Tree] #leetcode
class Solution {
public boolean isValidBST(TreeNode root) {
return dfs(root, Long.MIN_VALUE, Long.MAX_VALUE);
}
private boolean dfs(TreeNode node, long left, long right) {
if ( node == null ) return true;
return left < node.val && right > node.val && dfs(node.left, left, node.val) && dfs(node.right, node.val, right);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment