class Solution {
public:
int getdepth(TreeNode* root){
if(root==NULL) return 0;
int left=getdepth(root->left);
int right=getdepth(root->right);
return left>right?(left+1):(right+1);
}
bool isBalanced(TreeNode* root) {
if(root==NULL) return true;
int leftdepth=getdepth(root->left);
int rightdepth=getdepth(root->right);
int result=abs(leftdepth-rightdepth);
if(result>1) return false;
else
return isBalanced(root->left)&&isBalanced(root->right);
}
};
110. Balanced Binary Tree
最新推荐文章于 2022-07-18 20:53:17 发布