How to determine if binary tree is balanced?
Posted
by user69514
on Stack Overflow
See other posts from Stack Overflow
or by user69514
Published on 2009-04-13T01:58:06Z
Indexed on
2010/04/07
21:13 UTC
Read the original article
Hit count: 875
It's been a while from those school years. Got a job as IT specialist at a hospital. Trying to move to do some actual programming now. I'm working on binary trees now, and I was wondering what would be the best way to determine if the tree is height-balanced.
I was thinking of something along this:
public boolean isBalanced(Node root){
if(root==null){
return true; //tree is empty
}
else{
int lh = root.left.height();
int rh = root.right.height();
if(lh - rh > 1 || rh - lh > 1){
return false;
}
}
return true;
}
Is this a good implementation? or am I missing something?
© Stack Overflow or respective owner