class Solution { public: bool isBalanced(TreeNode* root) { int depth = 0; return Balanced(root,depth); } bool Balanced(TreeNode* root,int& depth){ if(root == NULL){ depth = 0; return true; } int left,right; if(Balanced(root->left,left) && Balanced(root->right,right)){ int gap = left - right; if(gap <= 1 && gap >= -1){ depth = left > right ? left + 1 : right + 1; return true; } } return false; } };