classSolution{public:boolans;boolisSameTree(TreeNode*p,TreeNode*q){// Note: The Solution object is instantiated only once and is reused by each test case.
ans=true;check(p,q);returnans;}voidcheck(TreeNode*p,TreeNode*q){if((p==NULL&&q!=NULL)||(p!=NULL&&q==NULL)){ans=false;return;}elseif(p==NULL&&q==NULL){return;}else{if(p->val!=q->val){ans=false;return;}else{check(p->left,q->left);check(p->right,q->right);}}}};