DEV Community

Cover image for 110. Balanced Binary Tree
Harsh Rajpal
Harsh Rajpal

Posted on

110. Balanced Binary Tree

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

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

Solution:

Algorithm:

  1. If the root is null, return true.
  2. Create a variable leftHeight to store the height of the left subtree.
  3. Create a variable rightHeight to store the height of the right subtree.
  4. Return true if the absolute difference between leftHeight and rightHeight is less than or equal to 1 and the left subtree is balanced and the right subtree is balanced, otherwise return false.

Code:

public class Solution {
    public boolean isBalanced(TreeNode root) {
        if (root == null) {
            return true;
        }
        int leftHeight = height(root.left);
        int rightHeight = height(root.right);
        return Math.abs(leftHeight - rightHeight) <= 1 && isBalanced(root.left) && isBalanced(root.right);
    }

    private int height(TreeNode root) {
        if (root == null) {
            return 0;
        }
        return 1 + Math.max(height(root.left), height(root.right));
    }

}
Enter fullscreen mode Exit fullscreen mode

Time Complexity:
O(n^2) where n is the number of nodes in the tree.
Space Complexity:
O(n) where n is the number of nodes in the tree.

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay