Bird
Raised Fist0
DSA Pythonprogramming~20 mins

Mirror a Binary Tree in DSA Python - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Mirror Tree Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Mirroring a Simple Binary Tree
What is the output of the following code after mirroring the binary tree? The output is the inorder traversal of the mirrored tree.
DSA Python
class Node:
    def __init__(self, val):
        self.val = val
        self.left = None
        self.right = None

def mirror(root):
    if root is None:
        return None
    root.left, root.right = mirror(root.right), mirror(root.left)
    return root

def inorder(root):
    return inorder(root.left) + [root.val] + inorder(root.right) if root else []

root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)

mirror(root)
print(inorder(root))
A[4, 2, 5, 1, 3]
B[2, 4, 1, 3, 5]
C[5, 3, 4, 1, 2]
D[3, 1, 5, 2, 4]
Attempts:
2 left
💡 Hint
Think about swapping left and right children recursively and then doing inorder traversal.
🧠 Conceptual
intermediate
1:30remaining
Effect of Mirroring on Tree Height
After mirroring a binary tree, which statement about the tree's height is always true?
AThe height becomes zero because the tree is reversed.
BThe height doubles compared to the original tree.
CThe height remains exactly the same as the original tree.
DThe height becomes the number of leaf nodes.
Attempts:
2 left
💡 Hint
Mirroring swaps left and right children but does not add or remove nodes.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Mirror Function
What error will the following mirror function cause when run on a binary tree?
DSA Python
def mirror(root):
    if root is None:
        return
    temp = root.left
    root.left = root.right
    root.right = temp
    mirror(root.left)
    mirror(root.right)

# Tree setup omitted for brevity
ANo error; function works correctly.
BRecursion causes infinite loop leading to maximum recursion depth exceeded error.
CAttributeError because root.left or root.right might be None.
DTypeError due to swapping nodes incorrectly.
Attempts:
2 left
💡 Hint
Trace the execution step-by-step on a small binary tree to verify.
🚀 Application
advanced
1:30remaining
Mirror a Binary Tree and Check Symmetry
Given a binary tree, after mirroring it, which of the following statements is true about the tree's symmetry?
AThe mirrored tree is symmetric only if the original tree is symmetric.
BThe mirrored tree is never symmetric regardless of the original tree.
CThe mirrored tree is symmetric only if the original tree is a perfect binary tree.
DThe mirrored tree is always symmetric if the original tree was symmetric.
Attempts:
2 left
💡 Hint
Think about what symmetry means and how mirroring affects it.
❓ Predict Output
expert
2:30remaining
Output of Mirroring and Level Order Traversal
What is the output of the level order traversal after mirroring the following binary tree?
DSA Python
from collections import deque

class Node:
    def __init__(self, val):
        self.val = val
        self.left = None
        self.right = None

def mirror(root):
    if root is None:
        return None
    root.left, root.right = mirror(root.right), mirror(root.left)
    return root

def level_order(root):
    if not root:
        return []
    result = []
    queue = deque([root])
    while queue:
        node = queue.popleft()
        result.append(node.val)
        if node.left:
            queue.append(node.left)
        if node.right:
            queue.append(node.right)
    return result

root = Node(10)
root.left = Node(20)
root.right = Node(30)
root.left.left = Node(40)
root.left.right = Node(50)
root.right.right = Node(60)

mirror(root)
print(level_order(root))
A[10, 20, 30, 40, 50, 60]
B[10, 30, 20, 60, 50, 40]
C[10, 30, 20, 50, 40, 60]
D[10, 20, 30, 60, 50, 40]
Attempts:
2 left
💡 Hint
Mirroring swaps left and right children at every node. Then level order visits nodes level by level from left to right.

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

  1. Step 1: Understand the mirror operation

    Mirroring a binary tree means swapping the left and right children of every node in the tree.
  2. Step 2: Compare with other options

    Deleting leaves, reversing values, or sorting nodes are different operations and do not create a mirror image.
  3. Final Answer:

    Swapping the left and right children of every node -> Option A
  4. 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

  1. Step 1: Identify function purpose and return type

    The mirror function modifies the tree in place and returns nothing, so return type is None.
  2. Step 2: Check syntax correctness

    def mirror(root) -> None: uses correct Python syntax with type hint for no return value.
  3. Final Answer:

    def mirror(root) -> None: -> Option C
  4. 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
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)
medium
A. 1 3
B. 3 2
C. 2 3
D. 3 1

Solution

  1. Step 1: Understand initial tree structure

    Root is 1, left child 2, right child 3.
  2. Step 2: Apply mirror function

    Mirror swaps left and right children at root, so left becomes 3 and right becomes 2.
  3. Final Answer:

    3 2 -> Option B
  4. Quick Check:

    Swapped children = 3 2 [OK]
Hint: Mirror swaps children, so left=3 and right=2 after mirror [OK]
Common Mistakes:
  • Not swapping children correctly
  • Printing values before mirror call
  • Confusing left and right child values
4. Find the error in this mirror function code:
def mirror(root):
    if root is None:
        return
    mirror(root.left)
    mirror(root.right)
    root.left = root.right
    root.right = root.left
medium
A. Swapping left and right children is done incorrectly
B. Base case is missing
C. Function does not call itself recursively
D. No return statement in function

Solution

  1. Step 1: Analyze swapping logic

    Assigning root.left = root.right then root.right = root.left sets both to same child, losing original left.
  2. Step 2: Correct swapping method

    Use simultaneous swap: root.left, root.right = root.right, root.left to avoid overwriting.
  3. Final Answer:

    Swapping left and right children is done incorrectly -> Option A
  4. Quick Check:

    Swap must be simultaneous to avoid overwrite [OK]
Hint: Swap children simultaneously to avoid overwriting [OK]
Common Mistakes:
  • Swapping children one by one causing data loss
  • Forgetting base case for recursion
  • Not swapping children at all
5. Given a binary tree, write a function to check if the tree is symmetric (a mirror of itself). Which approach correctly uses the mirror concept?
hard
A. Traverse tree in preorder and compare with inorder traversal
B. Check if left and right subtrees have the same number of nodes
C. Check if all node values are equal in left and right subtrees
D. Compare left subtree with right subtree by recursively checking if left.left matches right.right and left.right matches right.left

Solution

  1. Step 1: Understand symmetry in binary trees

    A symmetric tree is a mirror of itself, so left subtree must mirror right subtree.
  2. Step 2: Use recursive comparison

    Check if left.left matches right.right and left.right matches right.left recursively to confirm mirror symmetry.
  3. Final Answer:

    Compare left subtree with right subtree by recursively checking if left.left matches right.right and left.right matches right.left -> Option D
  4. Quick Check:

    Symmetry = recursive mirror check [OK]
Hint: Symmetry means left subtree mirrors right subtree recursively [OK]
Common Mistakes:
  • Only comparing node counts, not structure
  • Comparing node values without structure
  • Using traversal orders that don't check mirror
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard