Invert mirrors both halves and swaps them. Same Tree walks two trees at once and stops at the first difference.
Two Day 9 problems, both one recursive idea deep. Watch the subtrees of invert trade sides as each call returns, then switch to same tree and watch p and q move together.
l
matched, returned true
returned false
empty subtree (nullptr)
Press Next to make the first call.
Each call hands back its subtree already mirrored. A mirror image puts the mirrored right half on the left and the mirrored left half on the right, so the only work a call does itself is to put the two answers in the opposite slots.
Step to the moment right after root->left = invertTree(r). The right subtree has moved left, but root->right still points at it too: the old left subtree is no longer reachable from the tree at all. The dashed edge is the saved l, and it is the only thing still holding on. Without that copy, line 7 would read the new left child, mirror it back, and the old left half would be lost.
Read both children before you overwrite either.
This is the palindrome check again, on trees: two positions that move together and are compared at each step. p == q catches the case where both are empty. If only one is, the shapes differ.
Pick different shape: both trees hold 1 and 2, but one has 2 on the left and the other on the right, and the walk finds an empty subtree facing a node. Pick one deep leaf: the walk matches eight pairs and fails on the last. Pick different values: && stops at the first false, and everything to the right is never visited.