Search⌘ K
AI Features

Mirror Binary Tree Nodes

Explore how to mirror a binary tree by swapping left and right child nodes at each step through a post-order traversal. Understand the algorithm's time and space complexity while practicing tree manipulation techniques essential for coding interviews.

Statement

Given the root node of a binary tree, swap the left and right children for each node, such that the binary tree becomes a mirror image of itself.

Example

The below example shows what the mirrored binary tree looks like:

G cluster_1 Original binary tree cluster_2 Mirrored binary tree root 100 node1 50 root->node1 node2 200 root->node2 node3 25 node1->node3 node4 75 node1->node4 node6 350 node2->node6 root2 100 node12 200 root2->node12 node22 50 root2->node22 node32 350 node12->node32 node52 75 node22->node52 node62 25 node22->node62

Sample input

The input list below represents the level-order traversal of the original binary tree:

[100, 50, 200, 25, 75, 350]

Expected output

The sequence below represents the in-order traversal of the mirrored binary tree:

350, 200, 100, 75, 50, 25

Try it yourself

Note: The binary tree node’s class has members left and right to store references to other nodes, along with the member data to hold the node’s value. ...

Files
main.cpp
BinaryTree.cpp
BinaryTreeNode.cpp
#include <iostream>
#include "BinaryTree.cpp"
using namespace std;
void MirrorBinaryTree(BinaryTreeNode* node) {
// TODO: Write - Your - Code
}

Solution

...