Invert Binary Tree
Mirror a tree by swapping the children of every node
DSA, Easy, Trees
Which structure or algorithm, and what does it cost.
You get a binary tree tree. Your code may take the root node or the list. Each node has val, left and right, null when missing. Either is fine; no conversion is needed. The examples use a level-order list. It holds values row by row, left to right. Null marks a missing child. A missing node has no entries below it. An empty list is an empty tree. Mirror the tree by swapping the left and right child of every node. Return the root of the mirrored tree, or the level-order list without trailing nulls; either is fine.
Examples
Constraints
- 0 <= number of nodes <= 30,000.
- Each value is a whole number between -5,000 and 5,000.
- The tree can be one long chain.
Your answer must contain
- The simple method and its cost; the best method and why it is correct; code; time and memory cost; a hand trace of example 1.
Target Aim for O(n) time.
Hints
Try the problem first. Open one hint at a time.
Hint 1 a nudge
Look at one node and its two children. What would that node look like in a mirror?
Hint 2 the approach
Every node needs the same change, and the order of visits does not matter. How can you reach each node exactly once?
Hint 3 the key step
Keep a stack that starts with the root. Pop a node, swap its two children, and push the children that exist. Stop when the stack is empty.
Walkthrough
Full walkthrough brute force to best
Brute force
Copy the tree into a new tree. Give each copied node the right child of the original as its left child, and the left child as its right child. This visits each node once, so it is O(n) time, but it builds a second tree and needs O(n) extra memory.
Best method
Mirroring a tree is the same swap at every node: left and right trade places. The swap at one node does not depend on any other node, so any visiting order works. Keep a stack of nodes still to visit, starting with the root. Pop a node, swap its children, then push the children that exist. Every node is popped once, so the work is linear. The change happens in place, so no copy is needed, and an explicit stack cannot overflow on a very deep chain.
Hand trace
Run on [6, 2, 9, 1, null, 7, 12]. Stack [6]. Pop 6 and swap: its children are now 9 on the left and 2 on the right. Push 9 and 2. Stack [9, 2]. Pop 2: its only child 1 moves from left to right. Push 1. Stack [9, 1]. Pop 1: a leaf, nothing to swap. Pop 9: its children become 12 on the left and 7 on the right. Push both. Pop 7 and 12: leaves. The stack is empty. Reading the tree by rows gives [6, 9, 2, 12, 7, null, 1].
Code
def invert_tree(root):
stack = [root] if root else []
while stack:
node = stack.pop()
node.left, node.right = node.right, node.left
stack.extend(child for child in (node.left, node.right) if child)
return rootTime and memory
O(n) time, because each node is pushed and popped once. O(h) extra memory for the stack, where h is the height of the tree: O(n) for a chain, O(log n) for a balanced tree.
Common mistakes
- Swapping the values of the two children instead of the children themselves. The subtrees stay where they were, so the tree is not mirrored.
- Swapping only at the root. Every node needs its own swap, all the way down.
- Pushing a missing child onto the stack and then reading its fields. Push only the children that exist, and handle the empty tree first.