C++ approach, improvisations are welcome


#1
/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
 int height(TreeNode *root)
 {
     if(!root)
        return 0;
    return 1 + max(height(root->left),height(root->right));
 }
 
int Solution::isBalanced(TreeNode* A) 
{
    if(!A)
        return 1;
    
    int left = height(A->left);
    int right = height(A->right);
    
    if(abs(left-right)<=1 && isBalanced(A->left) && isBalanced(A->right))
        return 1;
    return 0;
}

#2

It is O(N^2) in worst case