How Can I Determine If a Binary Tree Is Height-Balanced?

Question

How can I determine if a binary tree is height-balanced?

public boolean isBalanced(Node root) {
    if (root == null) {
        return true;  // tree is empty
    }
    int lh = height(root.left);
    int rh = height(root.right);
    return Math.abs(lh - rh) <= 1 && isBalanced(root.left) && isBalanced(root.right);
}

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

Answer

Determining if a binary tree is height-balanced involves ensuring that the heights of the two child subtrees of any node differ by no more than one. A balanced tree provides optimal performance for operations such as insertion, deletion, and lookup. This process can be implemented efficiently using a recursive approach.

public boolean isBalanced(Node root) {
    if (root == null) {
        return true;  // tree is empty
    }
    int lh = height(root.left);
    int rh = height(root.right);
    if (Math.abs(lh - rh) > 1) {
        return false;
    }
    return isBalanced(root.left) && isBalanced(root.right);
}

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

Causes

  • Unbalanced branches due to uneven heights of child nodes.
  • Improper insertion order affecting the tree structure.
  • Manipulation of the tree without following balance rules.

Solutions

  • Use a recursive function to calculate heights of subtrees while checking balance concurrently.
  • Implement self-balancing trees like AVL or Red-Black trees to maintain balance after each operation.

Common Mistakes

Mistake: Not checking both left and right subtrees for balance.

Solution: Ensure to check balance for both child nodes recursively.

Mistake: Returning early if only one subtree is of unwanted height.

Solution: Both subtrees' heights must be checked prior to returning.

Helpers

  • binary tree balance check
  • height-balanced binary tree
  • binary tree algorithms

Related Questions

⦿Understanding Float Data Type in Java: Definition and Common Issues

Learn about the float data type in Java its definition and how to resolve common errors like type mismatch. Expert insights and code examples included.

⦿How to Convert an ArrayList<Object> to ArrayList<String> in Java?

Learn how to effectively convert an ArrayList of Objects to an ArrayList of Strings in Java with clear steps and code examples.

⦿Why Does Comparing Boxed Long Values 127 and 128 Yield Different Results?

Understanding the comparison of boxed Long values in Java and why it fails for values 128 and above.

⦿Does the Order of Insertion Affect Retrieval in an ArrayList?

Learn about the insertion and retrieval order of elements in an ArrayList in Java and how it maintains the sequence.

⦿How to Retrieve Multiple Values from a Spring .properties File as an Array

Learn how to correctly load multiple values from a Spring .properties file into a list using the Spring framework.

⦿What is the Correct Maven Environment Variable: MAVEN_HOME, MVN_HOME, or M2_HOME?

Learn the correct Maven environment variable name among MAVENHOME MVNHOME and M2HOME including their differences and usage.

⦿How to Recreate Database Before Each Test in Spring Boot?

Learn how to ensure your Spring Boot application recreates the database before each test execution. Discover best practices and troubleshoot common issues.

⦿Understanding NullPointerException in Boolean.valueOf() Method

Learn why Boolean.valueOf can throw NullPointerException in Java and how to avoid it with proper code practices.

⦿Should Logger Be Declared as Private Static or Non-Static?

Explore whether to declare a logger as private static or nonstatic including pros cons and best practices for logging in Java.

⦿Understanding the Difference Between a.getClass() and A.class in Java

Explore the nuances between a.getClass and A.class in Java including performance considerations and usage scenarios.

© Copyright 2025 - CodingTechRoom.com