标签:
Given a binary tree , determine if it is height-balanced
implement method is in java language,iterator through the height rather than the depth
public int height(TreeNode root){
if(root!=null)
return 0;
int lh = height(root.left);
int rh = height(root.right);
if(lh!=-1&&rh!=-1&&Math.abs(lh-rh)<2)
return 1+Math.max(lh,rh);
else
return -1;
}
public boolean isBalanced(TreeNode root){
return height(root)!=-1;
}
标签:
原文地址:http://www.cnblogs.com/leilei-lily/p/4725448.html