Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 859
  • Last Modified:

Determining if binary tree is height balanced.

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
ubuntuguy
Asked:
ubuntuguy
1 Solution
 
objectsCommented:
>                        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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now