class Solution {
public:
int height(TreeNode* root){//求当前结点高度
if(root==NULL){
return 0;
}
else
return max(height(root->left),height(root->right))+1;
}
bool isBalanced(TreeNode* root) {
if(root==NULL){
return true;
}
else{
return isBalanced(root->left)&&isBalanced(root->right)&&(abs(height(root->left)-height(root->right))<2);
}
}
};
判断是否是平衡二叉树
最新推荐文章于 2024-07-18 13:20:33 发布