Problem. Given the root of a binary tree, invert it (produce its mirror image) and return the root.
4 4
/ \ / \
2 7 → 7 2
/ \ / \ / \ / \
1 3 6 9 9 6 3 1
A short problem with a useful lesson: the operation is local (swap two pointers at a node) and independent at every node, so any traversal that visits every node once works, in any order.
ML context. Mirroring shows up in data augmentation for structured inputs (flipping a parse tree or a scene graph left-to-right, like a horizontal image flip), in symmetry checks for tree-structured models, and as a sanity test of tree code. And it carries a warning: mirroring a decision tree changes its predictions unless every comparison is flipped too (Change D).