Solved

Determining if binary tree is height balanced.

Posted on 2009-04-12
1
849 Views
Last Modified: 2013-11-23
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;

		}

Open in new window

0
Comment
Question by:ubuntuguy
1 Comment
 
LVL 92

Accepted Solution

by:
objects earned 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Exception after setting jdbc session management 2 52
firstswap challenge 20 67
Checkbox and ListView in Android Layout 4 56
object oriented programming comparison 5 55
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now