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: int sub(TreeNode* root){ if(root==NULL)return 0; int h1=sub(root->left); int h2=sub(root->right); if(h1==-1||h2==-1)return -1; if(h1-h2>=-1&&h1-h2<=1){ return max(h1,h2)+1; } else return -1; } bool isBalanced(TreeNode *root) { // Note: The Solution object is instantiated only once and is reused by each test case. if(sub(root)!=-1)return true; else return false; } };