Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Leetcode Balanced Binary Tree problem solution

YASH PAL, 31 July 202419 January 2026

In this Leetcode Balanced Binary Tree problem solution, we have 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.

Leetcode Balanced Binary Tree problem solution

Leetcode Balanced Binary Tree problem solution in Python.

class Solution:
    def isBalanced(self, root: TreeNode) -> bool:
        d = {}
        def ifbalanced(node):
            if not node:
                d[node] = 0
                return True
            elif not ifbalanced(node.left) or not ifbalanced(node.right):
                return False
            else:
                d[node] = max(d[node.left],d[node.right])+1
                return True if abs(d[node.left]-d[node.right]) <2 else False

        
        return ifbalanced(root)

Balanced Binary Tree problem solution in Java.

class Solution {
    int max = 0;
    public boolean isBalanced(TreeNode root) {
        depth(root);
        return max <= 1; 
    }
    public int depth(TreeNode root) {
        if(root==null) return 0;
        int left = depth(root.left);
        int right = depth(root.right);
        max = Math.max(Math.abs(left-right), max);
        return 1 + Math.max(left, right);
    }
}

Problem solution in C++.

class Solution {
public:
    int height(TreeNode* root){
        if(root==NULL)
            return 0;
        return max(height(root->left),height(root->right)) + 1;
    }
    bool isBalanced(TreeNode* root) {
        if(root==NULL)
            return true;
        return abs(height(root->left) - height(root->right))<=1 && isBalanced(root->right) && isBalanced(root->left);
    }
};

Problem solution in C.

bool isBalanced(struct TreeNode* root) {
    bool ans = true;
    isBalancedHelper(root, &ans);
    return ans;
}

int isBalancedHelper(struct TreeNode * root, bool *answer) {
    if(!root) return 0;
    
    int left = isBalancedHelper(root -> left, answer);
    int right = isBalancedHelper(root -> right, answer);
    
    if(abs(left - right) > 1) {
        *answer = false;
    }
    
    return left > right ? left + 1 : right + 1;
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

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

CLOSE ADS
CLOSE ADS

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes