Solved

Determining if binary tree is height balanced.

Posted on 2009-04-12
1
851 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to get all the API from website? 11 82
JDeveloper 12c for 32 bit 4 84
numbers ascending pyramid 101 191
Chrome and Firefox Java 5 23
Article by: Nadia
Linear search (searching each index in an array one by one) works almost everywhere but it is not optimal in many cases. Let's assume, we have a book which has 42949672960 pages. We also have a table of contents. Now we want to read the content on p…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
The viewer will learn how to implement Singleton Design Pattern in Java.

813 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