Here is solution in C++


#1

int height(TreeNode* root ,bool &flag)
{

 if(!flag)
 return 0;
 if (root==NULL)
 return 0;
 
 else{
     int l= height(root->left,flag);
     int r=height(root->right,flag);
 
     if(abs(l-r)>=2)
     flag=false;
     
     return 1+max(height(root->left,flag),height(root->right,flag));
     
 }

}

int Solution::isBalanced(TreeNode* A) {

if (A==NULL)
return 1;
bool flag =true;
int x=height(A,flag);
return flag;

}


#2
pair<bool, int> solve(TreeNode* node, int depth)
{
    if(node==NULL)
        return make_pair(true, depth-1);
    pair<bool, int> lAns=solve(node->left, depth+1);
    pair<bool, int> rAns=solve(node->right, depth+1);
    if(lAns.first && rAns.first)
        return make_pair((abs(rAns.second-lAns.second)<=1), max(lAns.second,rAns.second));
    else
        return make_pair(false, -1);
}
int Solution::isBalanced(TreeNode* A) 
{ return solve(A, 0).first; }