What is wrong with my solution?


  • 0
    M

    /**

    • Definition for binary tree
    • public class TreeNode {
    • int val;
      
    • TreeNode left;
      
    • TreeNode right;
      
    • TreeNode(int x) { val = x; }
      
    • }
      */

    public class Solution {
    int minn=Integer.MAX_VALUE;
    int maxx=Integer.MIN_VALUE;
    public boolean isBalanced(TreeNode root)
    {
    if(root==null) return true;
    if((root.left==null && root.right!=null) || (root.left!=null && root.right==null)) minn=0;
    balancer(root,0);
    if((maxx-minn)<=1) return true;
    else return false;
    }
    public void balancer(TreeNode root,int count)
    {
    if(root==null||(root.left==null && root.right==null))
    {

            if(count<minn) minn=count;
            if(count>maxx) maxx=count;
        }
        else
        {
             balancer(root.left,count+1);
             balancer(root.right,count+1);
        }
    }
    

    }


  • 0
    V

    please format the question correctly


  • 0
    S

    Thansk @vivek.bits for pointing this out.


Log in to reply
 

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