Solved

Determining if binary tree is height balanced.

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
powerN  challenge 3 47
network + 7 73
pairs challenge 5 44
Eclipse Neon and jdk 1.8.0 11 67
Article by: Nadia
Suppose you use Uber application as a rider and you request a ride to go from one place to another. Your driver just arrived at the parking lot of your place. The only thing you know about the ride is the license plate number. How do you find your U…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.

705 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

19 Experts available now in Live!

Get 1:1 Help Now