Balanced Binary Tree – Leetcode Solution

In this post, we are going to solve the 110. Balanced Binary Tree problem of Leetcode. This problem 110. Balanced Binary Tree is a Leetcode easy level problem. Let’s see the code, 110. Balanced Binary Tree – Leetcode Solution.

Problem

Given a binary tree, determine if it is height-balanced.

For this problem, a height-balanced binary tree is defined as:

  • a binary tree in which the left and right subtrees of every node differ in height by no more than 1.

Example 1 :

Input: root = [3,9,20,null,null,15,7]
Output: true

Example 2 :

Input: root = [1,2,2,3,3,null,null,4,4]
Output: false

Example 3 :

Input: root = []
Output: true

Constraints

  • The number of nodes in the tree is in the range [0, 5000].
  • -104 <= Node.val <= 104

Now, let’s see the code of 110. Balanced Binary Tree – Leetcode Solution.

Balanced Binary Tree – Leetcode Solution

110. Balanced Binary Tree – Solution in Java

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int height(TreeNode root){
        if(root==null) return 0;
        
        int lht = height(root.left);
        int rht = height(root.right);
        
        return Math.max(lht,rht)+1;
        
    }
    public boolean isBalanced(TreeNode root) {
        if(root == null) return true;
        if(root.left == null && root.right == null) return true;
        
        int lht = height(root.left);
        int rht = height(root.right);
        
        if(Math.abs(lht-rht)>1){
            return false;
        }
        
        boolean checkLeft = isBalanced(root.left);
        boolean checkRight = isBalanced(root.right);
        
        if(checkLeft == false || checkRight == false) return false;
        
        return true;
    }
}

110. Balanced Binary Tree – Solution in C++

class Solution {
public:
    bool ans;
    int checkBalance(TreeNode* root){
        if(!root)
            return 0;
        if(!ans) // if Answer is already False then return it.
            return 0;
        int leftSubTree = checkBalance(root->left);
        int rightSubTree = checkBalance(root->right);
        if(abs(leftSubTree-rightSubTree) > 1){
            ans = false;
        }
        return 1 + max(leftSubTree, rightSubTree);
    }
    bool isBalanced(TreeNode* root){
        ans = true;
        int temp = checkBalance(root);
        return ans;
    }
};

110. Balanced Binary Tree – Solution in Python

class Solution(object):
    def isBalanced(self, root):
            
        def check(root):
            if root is None:
                return 0
            left  = check(root.left)
            right = check(root.right)
            if left == -1 or right == -1 or abs(left - right) > 1:
                return -1
            return 1 + max(left, right)
            
        return check(root) != -1

Note: This problem 110. Balanced Binary Tree is generated by Leetcode but the solution is provided by CodingBroz. This tutorial is only for Educational and Learning purpose.

Leave a Comment

Your email address will not be published. Required fields are marked *