Mirror a Binary Tree in DSA Python - Time & Space Complexity
We want to know how long it takes to flip a binary tree so that left and right children swap places.
How does the time needed grow as the tree gets bigger?
Analyze the time complexity of the following code snippet.
def mirror_tree(node):
if node is None:
return
node.left, node.right = node.right, node.left
mirror_tree(node.left)
mirror_tree(node.right)
This code swaps left and right children of every node in the tree recursively.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Visiting each node once to swap its children.
- How many times: Exactly once per node in the tree.
Each node is visited once, so the work grows directly with the number of nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 swaps |
| 100 | About 100 swaps |
| 1000 | About 1000 swaps |
Pattern observation: Doubling the nodes roughly doubles the work.
Time Complexity: O(n)
This means the time to mirror the tree grows linearly with the number of nodes.
[X] Wrong: "The time depends on the height of the tree only."
[OK] Correct: Even if the tree is tall or short, every node must be visited once, so total time depends on total nodes, not just height.
Understanding this helps you reason about recursive tree problems and shows you can analyze how work scales with input size.
"What if we changed the code to use an iterative approach with a stack? How would the time complexity change?"
Practice
Mirror a Binary Tree means:Solution
Step 1: Understand the mirror operation
Mirroring a binary tree means swapping the left and right children of every node in the tree.Step 2: Compare with other options
Deleting leaves, reversing values, or sorting nodes are different operations and do not create a mirror image.Final Answer:
Swapping the left and right children of every node -> Option AQuick Check:
Mirror = Swap children [OK]
- Thinking mirror means reversing node values only
- Confusing mirror with tree traversal
- Assuming mirror deletes nodes
root?Solution
Step 1: Identify function purpose and return type
The mirror function modifies the tree in place and returns nothing, so return type is None.Step 2: Check syntax correctness
def mirror(root) -> None: uses correct Python syntax with type hint for no return value.Final Answer:
def mirror(root) -> None: -> Option CQuick Check:
Function returns nothing = None [OK]
- Using wrong return type like int or str
- Omitting parentheses in function definition
- Missing colon at end of function header
mirror(root)? Assume the tree is:1
/ \
2 3
class Node:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
def mirror(root) -> None:
if root is None:
return
root.left, root.right = root.right, root.left
mirror(root.left)
mirror(root.right)
root = Node(1)
root.left = Node(2)
root.right = Node(3)
mirror(root)
print(root.left.val, root.right.val)Solution
Step 1: Understand initial tree structure
Root is 1, left child 2, right child 3.Step 2: Apply mirror function
Mirror swaps left and right children at root, so left becomes 3 and right becomes 2.Final Answer:
3 2 -> Option BQuick Check:
Swapped children = 3 2 [OK]
- Not swapping children correctly
- Printing values before mirror call
- Confusing left and right child values
def mirror(root):
if root is None:
return
mirror(root.left)
mirror(root.right)
root.left = root.right
root.right = root.leftSolution
Step 1: Analyze swapping logic
Assigning root.left = root.right then root.right = root.left sets both to same child, losing original left.Step 2: Correct swapping method
Use simultaneous swap: root.left, root.right = root.right, root.left to avoid overwriting.Final Answer:
Swapping left and right children is done incorrectly -> Option AQuick Check:
Swap must be simultaneous to avoid overwrite [OK]
- Swapping children one by one causing data loss
- Forgetting base case for recursion
- Not swapping children at all
Solution
Step 1: Understand symmetry in binary trees
A symmetric tree is a mirror of itself, so left subtree must mirror right subtree.Step 2: Use recursive comparison
Check if left.left matches right.right and left.right matches right.left recursively to confirm mirror symmetry.Final Answer:
Compare left subtree with right subtree by recursively checking if left.left matches right.right and left.right matches right.left -> Option DQuick Check:
Symmetry = recursive mirror check [OK]
- Only comparing node counts, not structure
- Comparing node values without structure
- Using traversal orders that don't check mirror
Ready to prove it?
Pass the test and this pattern moves unknown
