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.
class Solution {
public:
bool is_balanced = true;
int checkHieght(TreeNode *root) {
int lh = 0, rh = 0;
if(root == NULL || is_balanced == false) return 0;
if(root->left != NULL && is_balanced == true)
lh = checkHieght(root->left) + 1;
if(root->right != NULL && is_balanced == true)
rh = checkHieght(root->right) + 1;
if(abs(lh - rh) > 1) is_balanced = false;
return lh > rh ? lh : rh;
}
bool isBalanced(TreeNode *root) {
checkHieght(root);
return is_balanced;
}
};