O(N) solution without memoiztion


#1

Comment body goes here. class HelpNode{
public:
bool check;
int h;
HelpNode(){
this->h=0;
this->check=false;
}

};
HelpNode help(TreeNode* A){
if(A==NULL){
HelpNode a;
a.check=true;
a.h=0;
return a;
}
HelpNode l=help(A->left);
HelpNode r=help(A->right);
HelpNode res;
if(l.check&&r.check&&(abs(l.h-r.h)<=1))
res.check=true;
res.h=1+max(l.h,r.h);
return res;

}
int Solution::isBalanced(TreeNode* A) {
HelpNode a=help(A);
return a.check;
}