Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions mirror BT
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
void mirror_tree(BinaryTreeNode* root) {
if (root == nullptr) {
return;
}

// We will do a post-order traversal of the binary tree.

if (root->left != nullptr) {
mirror_tree(root->left);
}

if (root->right != nullptr) {
mirror_tree(root->right);
}

// Let's swap the left and right nodes at current level.

BinaryTreeNode* temp = root->left;
root->left = root->right;
root->right = temp;
}

int main(int argc, char* argv[]) {

BinaryTreeNode* root = create_random_BST(15);
display_level_order(root);
mirror_tree(root);
cout << endl << "Mirrored tree = " << endl;
display_level_order(root);
}