What's wrong with my java code for this question?


  • 0
    D
    public class Solution {
    public boolean isBalanced(TreeNode root) {
        int depth=0;
        return isBalanced(root,depth);
    }
    public boolean isBalanced(TreeNode node,int depth){
        if(node == null){
            return true;
        } 
        int leftDepth=0,rightDepth=0;
        if( isBalanced(node.left,leftDepth) && isBalanced(node.right,rightDepth) ){
            int diff=leftDepth-rightDepth;
            if(diff<=1 && diff>=-1){
                depth=1+((leftDepth>rightDepth)?(leftDepth):(rightDepth));
                return true;
            }
            else return false;
        }
        return false;
    }
    

    }


  • 0
    J

    diff is always 0


Log in to reply
 

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