Java DFS solution

  • 1
    public class Solution {
        public boolean isValidBST(TreeNode root) {
            return isValidBST_dfs(root, Long.MIN_VALUE, Long.MAX_VALUE);
        private boolean isValidBST_dfs(TreeNode root, long min, long max){
            if(root == null) return true;
            if(root.val>= max || root.val<=min) return false;
            boolean left = isValidBST_dfs(root.left, min, root.val);
            boolean right = isValidBST_dfs(root.right, root.val, max);
            return left&right;

  • 0

    nice solution!

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.