Easy C++ approach using Unordered Map


#1

unordered_map<TreeNode*,int> mp;
int height(TreeNode* A){
if(A==NULL)
return 0;
if(mp.find(A)!=mp.end())
return mp[A];
return mp[A]=1+max(height(A->left),height(A->right));
}
int Solution::isBalanced(TreeNode* A) {
if(A==NULL)
return 1;
int lh=height(A->left);
int rh=height(A->right);
if(abs(lh-rh)>1)
return 0;
return isBalanced(A->left) && isBalanced(A->right);
}ere.