class Solution {
public:
bool isBalanced(TreeNode *root) {
return getheight(root)!=-1;
}
int getheight(TreeNode*root)
{
if(root==NULL)return 0;
int left=getheight(root->left);
if(left==-1)return -1;
int right=getheight(root->right);
if(right==-1)return -1;
return abs(left-right)>1?-1:max(left,right)+1;
}
};
网友评论