Check for Balanced Tree | Practice | GeeksforGeeks

Given a binary tree, find if it is height balanced or not. 
A tree is height balanced if difference between heights of left and right subtrees is not more than one for all nodes of tree. 
A height balanced tree
        1
     /     \
   10      39
  /
5
An unbalanced tree
        1
     /    
   10   
  /
5
Example 1:
Input:
      1
    /
   2
    \
     3 
Output: 0
Explanation: The max difference in height
of left subtree and right subtree is 2,
which is greater than 1. Hence unbalanced

Example 2:
Input:
       10
     /   \
    20   30 
  /   \
 40   60
Output: 1
Explanation: The max difference in height
of left subtree and right subtree is 1.
Hence balanced. 

Your Task:
You don't need to take input. Just complete the function isBalanced() that takes root node as parameter and returns true, if the tree is balanced else returns false.
Constraints:
1 <= Number of nodes <= 105
0 <= Data of a node <= 106
Expected time complexity: O(N)
Expected auxiliary space: O(h) , where h = height of tree
0
randeepsarma10in 8 hours
int d =0;
    int funct(Node*root){
        if(root==NULL)return 0;
        
        int lh=funct(root->left);
        int rh=funct(root->right);
        d=max(d,abs(lh-rh));
        return max(lh,rh)+1;
    }
    bool isBalanced(Node *root)
    {
        //  Your Code here
        funct(root);
        
        if(d>1)return false;
        return true;
        
    }
0
rajeshsuranain 3 hours
Java Solution from GeeksforGeeks article 
    class Height { int height; }
    boolean isBalanced(Node root, Height h) {
        if (root == null) {
            h.height = 0;
            return true;
        }
        Height lh = new Height(), rh = new Height();
        boolean lb = isBalanced(root.left, lh);
        boolean rb = isBalanced(root.right, rh);
        h.height = Math.max(lh.height, rh.height) + 1;
        return Math.abs(lh.height-rh.height)<2 && lb && rb; 
    }
    boolean isBalanced(Node root)
    {
        return isBalanced(root, new Height());
    }
0
swatibovi2 days ago
Anyone getting TLE for the recursive solution? 
----------------------------------------------------------------------------JAVA CODE
boolean isBalanced(Node root)   {       if(findHeight(root) == -1)           return false;    else        return true;   }      int findHeight(Node node){              if(node == null)           return 0;       if(findHeight(node.left) == -1 || findHeight(node.right) == -1)           return -1;       if(Math.abs(findHeight(node.left) - findHeight(node.right)) > 1)           return -1;              return 1 + Math.max(findHeight(node.left), findHeight(node.right));   } 
+1
ialtafshaikh5 days ago
Python Solution using max height of Binary Tree | TC O(n)
-----------------------------------------------------------------
class Solution:   def isBalanced(self,root):       return self.maxHeight(root) != -1      def maxHeight(self, root):              if(root is None):           return 0              lh = self.maxHeight(root.left)              if(lh == -1): return -1              rh = self.maxHeight(root.right)              if(rh == -1): return -1              # balanced tree condition       # abs()=> it will remove the negative sign of the number.         if(abs(lh-rh) > 1): return -1              return 1 + max(lh, rh)
0
satviksingh351 week ago
class Solution{   public:   int check(Node* node, int height, bool &ans){       if(node==NULL) return height;       int lh = check(node->left, height+1, ans);       int rh = check(node->right, height+1, ans);       if(abs(lh-rh)>1) ans = false;       return max(lh, rh);   }   bool isBalanced(Node *node)   {       int height=0;       bool ans = true;       check(node, height, ans);       return ans;   }};
0
itachinamikaze2211 week ago
 int helper(Node root)   {   if(root==null)          return 0;                int l=helper(root.left);      int r=helper(root.right);      if(l==-1||r==-1)      {          return -1;      }      if(Math.abs(l-r)>1)      {          return -1;      }      else      {          return 1+Math.max(l,r);      }  }    //Function to check whether a binary tree is balanced or not.  boolean isBalanced(Node root)  {       int res= helper(root);       if(res==-1)           return false;       else           return true;   }}
+1
tirtha19025681 week ago
class Tree
{
   boolean isBalanced(Node root)
   {
    if(root == null) return true;
    int lh = height(root.left);
    int rh = height(root.right);
    int diff = Math.abs(lh-rh);
    if(diff>1) return false;
    return isBalanced(root.left) && isBalanced(root.right);
   }
   
   int height(Node root){
       if(root == null) return 0;
       
       return Math.max(height(root.left),height(root.right))+1;
   }
}
0
anshulgupta966262 weeks ago
int check(Node *root){
        if(root == NULL){
            return 0;
        }
        else{
            int left = check(root->left);
            int right = check(root->right);
            if(right == -1 || left == -1){
                return -1;
            }
            if(abs(left-right) > 1){
                return -1;
            }
            return 1 + max(left, right);
        }
    }
    bool isBalanced(Node *root)
    {
        //  Your Code here
        int n = check(root);
        if(n == -1){
            return false;
        }
        return true;
    }
    
0
codingmonster2 weeks ago
bool result=true;   int height(Node *root)   {       if(!root)           return 0;       int l=height(root->left);       int r=height(root->right);       result=result && (abs(r-l)<=1?true:false);       return 1+max(l,r);   }   bool isBalanced(Node *root)   {       //  Your Code here       height(root);       return result;   }
0
himanshu0719cse193 weeks ago
int helper(Node root)   {       if(root==null)       {           return 0;       }       int l=helper(root.left);       int r=helper(root.right);       if(l==-1||r==-1)       {           return -1;       }       if(Math.abs(l-r)>1)       {           return -1;       }       else       {           return 1+Math.max(l,r);       }   }      //Function to check whether a binary tree is balanced or not.   boolean isBalanced(Node root)   {  return helper(root)==-1?false:true;   }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.