将二叉树转换为其镜像树的 C 函数

标签 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;

    /* 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;
  }
} 

关于将二叉树转换为其镜像树的 C 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31056729/

相关文章:

c - C 中的动态字符串数组结构

c - 将矩阵文本文件读入c中的数组

c - 使用 fflush(标准输入)

连续空间中的复杂结构偏移

c++ - 如何在 C++ 中序列化树结构?

python - 如何打印使用类实现的树数据结构?

c - c中的strlen错误

c - 在 C 中实现字典的快速方法

c++ - 如何在 C++ 中克隆对象?或者还有其他解决方案吗?

recursion - Groovy 递归函数在哪里存储结果?