Intuitive recursive solution in C++


#1

int height(TreeNode* root){
if(root==NULL)return -1;
return max(height(root->left),height(root->right)) + 1;
}
int solve(TreeNode* root){
if(root==NULL)return 1;
if(abs(height(root->left)-height(root->right))<=1){
return solve(root->left) && solve(root->right);
}
return 0;
}
int Solution::isBalanced(TreeNode* root) {
if(solve(root))return 1;
return 0;
}