Challenge - 5 Problems
Mirror Tree Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput 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))
Attempts:
2 left
💡 Hint
Think about swapping left and right children recursively and then doing inorder traversal.
✗ Incorrect
Mirroring swaps left and right children at every node. The inorder traversal of the mirrored tree visits nodes in the order: right subtree, root, left subtree of original tree. So the output is [3, 1, 5, 2, 4].
🧠 Conceptual
intermediateEffect of Mirroring on Tree Height
After mirroring a binary tree, which statement about the tree's height is always true?
Attempts:
2 left
💡 Hint
Mirroring swaps left and right children but does not add or remove nodes.
✗ Incorrect
Mirroring only swaps the left and right children at every node. It does not change the number of levels or nodes, so the height remains the same.
🔧 Debug
advancedIdentify 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
Attempts:
2 left
💡 Hint
Trace the execution step-by-step on a small binary tree to verify.
✗ Incorrect
No error occurs. The function correctly swaps the left and right children first and then recursively mirrors the subtrees pointed to by the new left and right. Since trees have no cycles, recursion terminates after the tree height.
🚀 Application
advancedMirror 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?
Attempts:
2 left
💡 Hint
Think about what symmetry means and how mirroring affects it.
✗ Incorrect
Mirroring flips the tree left to right. If the original tree is symmetric, its mirror is also symmetric. If the original is not symmetric, the mirror won't be symmetric either.
❓ Predict Output
expertOutput 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))
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.
✗ Incorrect
After mirroring, the left and right children of each node are swapped. The level order traversal visits nodes level by level, so the output reflects the mirrored structure.
