IsBalanced

来源:互联网 发布:七天网络如何注册账号 编辑:程序博客网 时间:2024/06/17 20:57
bool IsBalanced(BinaryTreeNode* pRoot, int* pDepth){    if(pRoot == NULL)    {        *pDepth = 0;        return true;    }     int left, right;    if(IsBalanced(pRoot->m_pLeft, &left)        && IsBalanced(pRoot->m_pRight, &right))    {        int diff = left - right;        if(diff <= 1 && diff >= -1)        {            *pDepth = 1 + (left > right ? left : right);            return true;        }    }     return false;}