Mirror a Binary Tree
📖 Scenario: You are working with a binary tree data structure that represents a family tree. Sometimes, you want to see the family tree flipped like looking in a mirror, where left and right children swap places at every level.
🎯 Goal: Build a program that creates a binary tree, then writes a function to mirror it by swapping left and right children of every node. Finally, print the mirrored tree in a readable format.
📋 What You'll Learn
Create a binary tree using a
TreeNode class with val, left, and right propertiesCreate a function
mirrorTree that takes the root of the tree and swaps left and right children recursivelyPrint the tree nodes in pre-order traversal after mirroring
💡 Why This Matters
🌍 Real World
Mirroring trees is useful in image processing, graphical transformations, and reversing hierarchical data views.
💼 Career
Understanding tree manipulation and recursion is essential for software engineering roles involving data structures, algorithms, and system design.
Progress0 / 4 steps