class Solution {
public:
bool IsBalanced(TreeNode *root,int &depth)
{
if(root==NULL)
{
return true;
}
int left=0;
int right=0;
if(IsBalanced(root->left,left)&&IsBalanced(root->right,right))
{
int dif=left-right;
if(dif<-1||dif>1)
{
return false;
}
depth=(left>right?left:right)+1;
return true;
}
return false;
}
bool IsBalanced_Solution(TreeNode* pRoot) {
int depth=0;
return IsBalanced(pRoot,depth);
}
};
剑指offer 平衡二叉树
最新推荐文章于 2020-03-27 12:08:30 发布