Simple Solution for the problem in C++


#1

int check_balanced(TreeNode* A)
{
if(!A)
return 0;
if(A->left == A->right)
return 1;
int left = check_balanced(A->left);
int right = check_balanced(A->right);
if(left == -1 || right == -1)
return -1;
int diff = left-right;
if(diff<0)
diff = -diff;
if(diff>1)
return -1;
if(left>right)
return left+1;
return right+1;
}
int Solution::isBalanced(TreeNode* A)
{
int check = check_balanced(A);
if(check!=-1)
return 1;
return 0;
}