3. What is the output of the following code after mirroring the binary tree?
struct Node {
int data;
Node* left;
Node* right;
};
void mirror(Node* root) {
if (!root) return;
mirror(root->left);
mirror(root->right);
Node* temp = root->left;
root->left = root->right;
root->right = temp;
}
// Tree before mirror:
// 1
// / \
// 2 3
// / \
// 4 5
Node* root = new Node{1, new Node{2, new Node{4, nullptr, nullptr}, nullptr}, new Node{3, nullptr, new Node{5, nullptr, nullptr}}};
mirror(root);
// Print inorder traversal
void inorder(Node* node) {
if (!node) return;
inorder(node->left);
std::cout << node->data << " ";
inorder(node->right);
}
inorder(root);