判断二叉树是否平衡

bool isBalanced(binaryTreeNode* root) 
{
    int height = 0;
    return isBalancedUtil(root, height);
}

bool isBalancedUtil(binaryTreeNode* root, int& height) 
{
    if (root == NULL) {
        height = 0;
        return true;
    }
    int lh = 0, rh = 0;
    bool isLeft = isBalancedUtil(root->m_pLeft, lh);
    bool isRight = isBalancedUtil(root->m_pRight, rh);
    height = (lh > rh ? lh : rh) + 1;
    return (abs(lh - rh) <= 1 && isLeft && isRight);
}

“`

你可能感兴趣的:(二叉树)