1ms recursive java code with global variable


  • 0
    C
    public class Solution {
        boolean bal;
        public boolean isBalanced(TreeNode root) {
            bal = true;
            int a = findDepth(root);
            return bal;
        }
        private int findDepth(TreeNode root){
            if (root==null) return 0;
            int dleft = findDepth(root.left) + 1;
            int dright = findDepth(root.right) + 1;
            if(Math.abs(dleft-dright)>1){
                bal = false;
                return 0;
            }
            return Math.max(dleft,dright);
        }
    }
    

Log in to reply
 

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