Balanced Binary Tree
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 f(TreeNode *root, int & height){ if (!root){ height = 0; return true; } int left_h = 0, right_h = 0; bool left_c = f(root->left,left_h); bool right_c = f(root->right,right_h); height = max(left_h,right_h) + 1; return left_c && right_c && abs(left_h - right_h) <= 1; } bool isBalanced(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function int height = 0; return f(root,height); } };