Given a binary tree, determine if it is height-balanced.
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
Difficulty:Easy
class Solution {
public:
int height(TreeNode* root){
if(root==NULL)
return 0;
int left,right;
left = height(root->left);
right = height(root->right);
return 1+max(left,right);
}
bool isBalanced(TreeNode* root) {
int lHeight, rHeight;
if(root==NULL)
return true;
lHeight = height(root->left);
rHeight = height(root->right);
if(abs(lHeight-rHeight)>1)
return false;
if(isBalanced(root->left)&&isBalanced(root->right))
return true;
else
return false;
}
};