class Solution {
public:
int depth(TreeNode *p){
if(p==NULL){
return 0;
}
int a=depth(p->left);
int b=depth(p->right);
if(a==-1||b==-1||fabs(a-b)>1){
return -1;
}
return max(a,b)+1;
}
bool isBalanced(TreeNode *root) {
int a=depth(root);
if(a==-1){
return false;
}
return true;
}
};
LeetCode - 110 Balanced Binary Tree
最新推荐文章于 2020-06-14 12:37:28 发布