Created
August 27, 2017 21:11
-
-
Save cixuuz/c7bea3857fcbf20cdc9e4e2040847b35 to your computer and use it in GitHub Desktop.
[98. Validate Binary Search Tree] #leetcode
This file contains hidden or 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
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