不平衡二叉树

问题描述:

可能重复:
How to determine if binary tree is balanced?不平衡二叉树

谁能告诉我如何检查是否二叉树是不平衡的,算法?

编辑:下面是Java的答案:

public int getDepth(Node n){ 
    if(n == null){ 
     return 0; 
    } 

    return 1 + (Math.max(getDepth(n.left), getDepth(n.right))); 
} 

public boolean isBalanced(Node n){ 
    if(n == null){ 
     return true; 
    } 

    int leftHeight = getDepth(n.left); 
    int rightHeight = getDepth(n.right); 

    return (Math.abs(leftHeight - rightHeight) <= 1 && isBalanced(n.left) && isBalanced(n.right)); 
}