Why the result is wrong?


  • 1
    F

    My code as follows:
    Test case is :{2,1,3}
    I tried in eclipse, it works very well ,however, here return false. I don't know why?

    public class Solution {
    public static boolean isbalanced = true;
    public boolean isBalanced(TreeNode root) {
           
            if(null == root)
            return true;
            getdepth(root,0);
            return isbalanced;
            
            
    
        
    }
    
    public int getdepth(TreeNode root, int dep){
        if(root ==null)
        return dep;
        int left = getdepth(root.left,1+dep);
        int right = getdepth(root.right,1+dep);
        if(Math.abs(left-right)>1){
            isbalanced= false;
        }
        return Math.max(left,right);
          
    }}

  • 1

    You should not use "static" in "public static boolean isbalanced = true;" .


  • 0
    M

    Still confused about it. Could spare time to tell me why can't add static here? Thx!


  • 0
    F

    The same question, should you please give us some details about it?


Log in to reply
 

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