Solved

Determining if binary tree is height balanced.

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Protect jar file - windows app 2 65
Oracle SQL syntax check  without executing 6 75
ejb message driven bean mdb creation steps 2 23
hashmap order 17 37
Prime numbers are natural numbers greater than 1 that have only two divisors (the number itself and 1). By “divisible” we mean dividend % divisor = 0 (% indicates MODULAR. It gives the reminder of a division operation). We’ll follow multiple approac…
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

829 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