Not a fan of recursive. Stack space is also space, otherwise we wouldn't have the "STACKOVERFLOW".

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