Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Determining if binary tree is height balanced.

Posted on 2009-04-12
Medium Priority
858 Views
I've been working on this method to determine if a binary tree is height balanced.  It doesn't work correctly.  Can you look at it and guide me in the right direction?  Thanks.

p.s. I'm using height() method which returns the height at a given node.
``````public boolean heightBalanced(BinaryTree<T> tree){
if(tree==null){
return true;  //tree is empty
}
else if(tree.left!=null && tree.right!=null){
int lh = tree.height(tree.left);
int rh = tree.height(tree.right);
if(lh - rh > 1 || rh - lh > 1){
return false;
}
tree.heightBalanced(tree.left);
tree.heightBalanced(tree.right);
}
return true;
}
``````
0
Question by:ubuntuguy
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
1 Comment

LVL 92

Accepted Solution

objects earned 2000 total points
ID: 24127805
>                        tree.heightBalanced(tree.left);
>                        tree.heightBalanced(tree.right);

looks like that shpuld be:

return tree.heightBalanced(tree.left) &&
tree.heightBalanced(tree.right);

0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The greatest common divisor (gcd) of two positive integers is their largest common divisor. Let's consider two numbers 12 and 20. The divisors of 12 are 1, 2, 3, 4, 6, 12 The divisors of 20 are 1, 2, 4, 5, 10 20 The highest number among the c…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.
###### Suggested Courses
Course of the Month11 days, 3 hours left to enroll