C 函数将二叉树转换为镜像树
如何将树转换为其镜像树。例如C 函数将二叉树转换为镜像树,c,data-structures,tree,binary-tree,C,Data Structures,Tree,Binary Tree,如何将树转换为其镜像树。例如 1 1 / \ / \ 2 3 to 3 2 / \ 4 4 执行后序遍历 void mirror(struct node* node) { if (node!=NULL) { struct node* temp
1 1
/ \ / \
2 3 to 3 2
/ \
4 4
执行后序遍历
void mirror(struct node* node)
{
if (node!=NULL)
{
struct node* temp;
/* do the subtrees */
mirror(node->left);
mirror(node->right);
/* swap the pointers in this node */
temp = node->left;
node->left = node->right;
node->right = temp;
}
}
解决方案使用递归将树转换为其镜像树