What if you could flip a whole tree perfectly with just a few lines of code?
Why Mirror a Binary Tree in DSA Python?
Imagine you have a family tree drawn on paper. You want to create a mirror image of it, flipping all the branches left to right. Doing this by redrawing every branch manually is tiring and easy to mess up.
Manually redrawing each branch means you must carefully swap every left and right child. It's slow, confusing, and you might forget some branches or swap incorrectly, making the tree wrong.
Mirroring a binary tree with code swaps the left and right children of every node automatically. This simple step repeated for all nodes quickly creates the perfect mirror without mistakes.
def mirror_tree(node): # Manually swap left and right for each node if node: temp = node.left node.left = node.right node.right = temp mirror_tree(node.left) mirror_tree(node.right)
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 lets you quickly flip any binary tree to its mirror image, useful in graphics, data transformations, and problem solving.
Think of flipping a family tree chart to see relationships from the opposite side or reversing decision trees in AI to explore alternative outcomes.
Manual flipping is slow and error-prone.
Code swaps children nodes recursively for a perfect mirror.
Useful for visualizing and transforming tree data easily.
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
