Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9210141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:59:56+00:00 2026-06-18T00:59:56+00:00

Check if a binary tree is balanced. The source code on the CTCI 5th:

  • 0

Check if a binary tree is balanced.

The source code on the CTCI 5th:

public class QuestionBrute {

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

public static boolean isBalanced(TreeNode root) {
    if (root == null) {
        return true;
    }
    int heightDiff = getHeight(root.left) - getHeight(root.right);
    if (Math.abs(heightDiff) > 1) {
        return false;
    }
    else {
        return isBalanced(root.left) && isBalanced(root.right);
    }
}

public static void main(String[] args) {
    // Create balanced tree
    int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    TreeNode root = TreeNode.createMinimalBST(array);
    System.out.println("Root? " + root.data);
    System.out.println("Is balanced? " + isBalanced(root));

    // Could be balanced, actually, but it's very unlikely...
    TreeNode unbalanced = new TreeNode(10);
    for (int i = 0; i < 10; i++) {
        unbalanced.insertInOrder(AssortedMethods.randomIntInRange(0, 100));
    }
    System.out.println("Root? " + unbalanced.data);
    System.out.println("Is balanced? " + isBalanced(unbalanced));
}
}

As the algorithm has to check the height of every node, and we don’t save the height in each recursion, the running time should be O(N^2).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T00:59:57+00:00Added an answer on June 18, 2026 at 12:59 am

    First of all let’s fix a bit your code. Your function to check if the root is balanced will not work simply because a binary tree is balanced if:

    maxHeight(root) - minHeight(root) <= 1
    

    I quote Wikipedia: “A balanced binary tree is commonly defined as a binary tree in which the depth of the two subtrees of every node differ by 1 or less”

    Your algorithm will give the wrong answer for this tree:

    A simple binary tree of size 9 and height 3, with a root node whose value is 2. The above tree is unbalanced and not sorted.

    When you call getHeight(Node7) it will return 3, and when you call getHeight(Node5) it will return 3 as well and since (0>1) == false you will return true 🙁

    To fix this all you have to do is to implement the int MinHeight(TreeNode node) the same way you did getHeight() but with Math.min()

    Now to your answer. In terms of runtime complexity whenever you call the getHeight() function from the root you are doing a DFS and since you have to visit all the nodes to find the height of the tree this algorithm will be O(N). Now it is true you execute this algorithm twice, when you call maxHeight(root) and minHeight(root) but since they are both O(N) (given that they do exactly what getHeight() does) the overall complexity will have C*N as an upper limit for some constant C and all N bigger than some N knot i.e. O(N) where N is the number of nodes of your tree 😉

    Cheers!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written the following code to check if a tree is a Binary
check this out: template <class T> class Test: public T { public: void TestFunc()
Check the below code int add(int a, int b) { return a + b;
A book I'm reading claims that one way to check whether a binary tree
How would you implement in Java the binary tree node class and the binary
what will be the algorithm to check two binary tree are isomorphic in nature?
How can I check that my binary tree doens't contain duplicates? Do you have
How to check if the binary representation of an integer is a palindrome?
Check this code : HTML : <div style="position: absolute; visibility: visible; width: 172px;"> <img
Check this code (My Custom Keyboard): -(IBAction) updateTextBackSpace:(id)sender { if([txtview.text length]>0) { NSString *deletedLastCharString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.