78. Invert Binary Tree
Mirror the tree by swapping every node's left and right subtrees. The transformation applies recursively at all depths, not just at the root.
Preserve existing node objects and values according to the mutation contract. Return the root of the transformed tree.
Example 1
Input
[4,2,7,1,3,6,9]
Output
same root; [4,7,2,9,6,3,1]
Explanation: Swap at every node, not only at the root.
Example 2
Input
[1,1,null,1]
Output
same root; [1,null,1,null,1]
Explanation: Duplicate values do not identify nodes; each original edge has an exact mirror destination.
Constraints
0 <= node count per tree <= 105-231 <= node.value <= 231 - 1- Each input is a proper binary tree: no cycles or shared child nodes. Null trees are permitted.
Hints
Hint 1
What two references belong to one local swap?
Hint 2
Save one child before overwriting either link.
Hint 3
Use an explicit stack, swap once per node, and push both nonnull new children.
Submissions
Your recent submissions for this problem.
Checking sign-in…
Discussion
Ask a question or share an approach. Posts are public; avoid personal information and mark solution spoilers clearly.
No discussion yet. Start the conversation with a question about this problem.
Checking sign-in…
Run your code to check the sample cases, or submit it to check the full validation suite.
Mirror each original identity
[4,2,7,1,3,6,9]
"T0"