Data Structures and Algorithms Interview Question: Implement a function that converts a binary tree into its mirror image. (recursive or non-recursive implementation)
Introduction: Implement a function that converts a binary tree into its mirror image. (recursive or non-recursive implementation)
The implementation idea of the algorithm is as follows:
- For the current node, swap its left and right subtrees.
- Recursively mirrors the left and right subtrees of this node.
The following is the code to convert a binary tree to its mirror image (non-recursive implementation) using C++, with detailed comments:
#include <iostream>
#include <stack>
using namespace std;
// 定义二叉树结构
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {
}
};
// 将一棵二叉树转换为它的镜像(非递归实现)
void mirror_iterative(TreeNode* root) {
if (root == nullptr) return;
stack<TreeNode*> s{
{
root}};
while (!s.empty()) {
TreeNode* node = s.top();
s.pop();
swap(node->left, node->right); // 交换左右子树
if (node->left != nullptr) s.push(node->left);
if (node->right != nullptr) s.push(node->right);
}
}
int main() {
// 构造一棵二叉树
TreeNode* root = new TreeNode(4);
root->left = new TreeNode(2);
root->right = new TreeNode(7);
root->left->left = new TreeNode(1);
root->left->right = new TreeNode(3);
root->right->left = new TreeNode(6);
root->right->right = new TreeNode(9);
// 转换并输出镜像结果
mirror_iterative(root);
cout << root->val << endl; // 4
cout << root->left->val << " "; // 7
cout << root->left->left->val << " "; // 9
cout << root->left->right->val << " "; // 6
cout << root->right->val << " "; // 2
cout << root->right->left->val << " "; // 3
cout << root->right->right->val << endl;// 1
return 0;
}
The following is the recursive implementation code of the same function, with detailed comments:
#include <iostream>
using namespace std;
// 定义二叉树结构
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {
}
};
// 将一棵二叉树转换为它的镜像(递归实现)
void mirror_recursive(TreeNode* root) {
if (root == nullptr) return;
swap(root->left, root->right); // 交换当前节点的左右子树
// 递归对左右子树进行镜像操作
mirror_recursive(root->left);
mirror_recursive(root->right);
}
int main() {
// 构造一棵二叉树
TreeNode* root = new TreeNode(4);
root->left = new TreeNode(2);
root->right = new TreeNode(7);
root->left->left = new TreeNode(1);
root->left->right = new TreeNode(3);
root->right->left = new TreeNode(6);
root->right->right = new TreeNode(9);
// 转换并输出镜像结果
mirror_recursive(root);
cout << root->val << endl; // 4
cout << root->left->val << " "; // 7
cout << root->left->left->val << " "; // 9
cout << root->left->right->val << " "; // 6
cout << root->right->val << " "; // 2
cout << root->right->left->val << " "; // 3
cout << root->right->right->val << endl;// 1
return 0;
}
Both codes build a binary tree in a way that defines the structure of the binary tree. mirror_iterative()
The function uses the stack for non-recursive implementation, thereby avoiding the stack depth of the function call and reducing the space complexity; while mirror_recursive()
the function is implemented using recursion, the code is more concise and easy to understand. The idea of the two functions is: for a node, after exchanging its left and right subtrees, recursively perform the same operation on its left and right subtrees.