Recursive solution in c++ Easy to understand


#1

int ht(TreeNode* A)
{
if(A==NULL)
return 0;

 return 1+max(ht(A->left),ht(A->right));

}
int Solution::isBalanced(TreeNode* A) {

if(A==NULL)
return 1;

int d=ht(A->left)-ht(A->right);
if(abs(d)>1)
return 0;

if((isBalanced(A->left))&&(isBalanced(A->right)))
return 1;

return 0;

}