【剑指offer】题39:平衡二叉树

来源:互联网 发布:全国省市县数据库 sql 编辑:程序博客网 时间:2024/06/06 03:00

bool func(TreeNode* pRoot, int& depth){    if (pRoot == NULL)    {        return true;    }    int left(0), right(0);    bool leftflag = func(pRoot->left, left);    bool rightflag = func(pRoot->right, right);    if (leftflag&&rightflag&&abs(left-right)<1)    {        depth = max(left, right) + 1;        return true;    }    else        return false;}bool IsBalanced_Solution(TreeNode* pRoot) {    if (pRoot==NULL)    {        return true;    }    int depth(0);    return func(pRoot, depth);}

原创粉丝点击