int TreeDepth(TreeNode* pRoot)
{
if(!pRoot)
return 0;
int nLeft = TreeDepth(pRoot->left);
int nRight = TreeDepth(pRoot->right);
return (nLeft > nRight) ? (nLeft + 1) : (nRight + 1);
}
class Solution {
public:
bool IsBalanced_Solution(TreeNode* pRoot) {
if (pRoot == NULL)
return true;
int nleft = TreeDepth(pRoot->left);
int nright = TreeDepth(pRoot->right);
if ((nleft - nright) > 1 || (nright -nleft) > 1 )
{
return false;
}
return IsBalanced_Solution(pRoot->left) && IsBalanced_Solution(pRoot->right);
}
};
输入一棵二叉树,判断该二叉树是否是平衡二叉树
最新推荐文章于 2021-12-29 21:41:16 发布