```
class Solution {
public:
bool isSameTree(TreeNode *p, TreeNode *q) {
if(p == NULL || q == NULL)
return p == q;
while(p != NULL && q != NULL){
if(p->val != q->val)
return false;
if((p->left)->val != (q->left)->val)
return false;
if((p->right)->val != (q->right)->val)
return false;
else{
return true == isSameTree(p->left, q->left) && true == isSameTree(p->right, q->right);
}
}
}
};
```

And can someone explain it to me that how recursion works?