Thanks to visit codestin.com
Credit goes to www.educative.io

...

/

Mirror Binary Tree Nodes

Mirror Binary Tree Nodes

Given the root node of a binary tree, swap the left and right children for each node.

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:

Codestin Search App Codestin Search App Original binary tree Codestin Search App Mirrored binary tree Codestin Search App 100 Codestin Search App 50 Codestin Search App Codestin Search App 200 Codestin Search App Codestin Search App 25 Codestin Search App Codestin Search App 75 Codestin Search App Codestin Search App 350 Codestin Search App Codestin Search App 100 Codestin Search App 200 Codestin Search App Codestin Search App 50 Codestin Search App Codestin Search App 350 Codestin Search App Codestin Search App 75 Codestin Search App Codestin Search App 25 Codestin Search App

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.

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

Solution

For this solution, we do a post-order (left, right, parent) traversal of the binary tree. The ...