📖 Scenario: You are working with a simple binary tree data structure. Sometimes, you need to flip the tree so that the left and right children of every node are swapped. This is called mirroring a binary tree.Imagine a family tree photo where you want to swap the left and right sides to see the mirror image.
🎯 Goal: Build a program that creates a binary tree, then writes a function to mirror it by swapping left and right children at every node, and finally prints the mirrored tree in a readable format.
📋 What You'll Learn
Create a binary tree with exactly 3 nodes: root with value 1, left child with value 2, and right child with value 3
Create a function called mirror_tree that takes the root node and swaps its left and right children recursively
Print the tree nodes in pre-order traversal (root, left, right) after mirroring
💡 Why This Matters
🌍 Real World
Mirroring binary trees is useful in image processing, graphics, and reversing tree-based data structures.
💼 Career
Understanding tree manipulations is important for software engineers working with data structures, algorithms, and system design.
Progress0 / 4 steps
1
Create the binary tree nodes
Create a class called Node with an __init__ method that takes value, left, and right parameters. Then create three nodes: root with value 1, left child with value 2, and right child with value 3. Connect the left and right children to the root node.
DSA Python
Hint
Define a Node class with value, left, and right. Then create root and assign its left and right children.
2
Define the mirror function
Define a function called mirror_tree that takes a node as input. If the node is not None, swap its left and right children, then recursively call mirror_tree on the left and right children.
DSA Python
Hint
Use recursion to swap left and right children for each node.
3
Call the mirror function on the root
Call the function mirror_tree with the variable root as the argument to mirror the entire tree.
DSA Python
Hint
Simply call mirror_tree(root) to start mirroring from the root node.
4
Print the mirrored tree in pre-order
Define a function called print_preorder that takes a node and prints its value, then recursively prints the left and right children in pre-order. Then call print_preorder with root to display the mirrored tree.
DSA Python
Hint
Print the node value first, then recursively print left and right children to get pre-order output.
Practice
(1/5)
1. What does mirroring a binary tree mean? Mirror a Binary Tree means:
easy
A. Swapping the left and right children of every node
B. Deleting all leaf nodes
C. Reversing the values of nodes in each level
D. Sorting the nodes in ascending order
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 A
Quick Check:
Mirror = Swap children [OK]
Hint: Mirror means swap left and right children at all nodes [OK]
Common Mistakes:
Thinking mirror means reversing node values only
Confusing mirror with tree traversal
Assuming mirror deletes nodes
2. Which of the following is the correct Python function header to define a recursive mirror function for a binary tree node named root?
easy
A. def mirror(root):
B. def mirror(root) -> int:
C. def mirror(root) -> None:
D. def mirror(root) -> str:
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 C
Quick Check:
Function returns nothing = None [OK]
Hint: Mirror modifies tree, returns nothing, so use -> None [OK]
Common Mistakes:
Using wrong return type like int or str
Omitting parentheses in function definition
Missing colon at end of function header
3. What will be the output of the following code after calling mirror(root)? Assume the tree is: 1 / \ 2 3