Balanced Binary Tree
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int getHeight(TreeNode *root){
if(root == NULL){
return 0;
}
int left = getHeight(root->left);
int right = getHeight(root->right);
return max(left,right) + 1;
}
bool isBalanced(TreeNode* root) {
if(root == NULL) return true;
int l = getHeight(root->left);
int r = getHeight(root->right);
//edge condition
return isBalanced(root->left)&&isBalanced(root->right)&&(abs(l-r) <= 1);
}
};