【博客328】求二叉树的镜像

    技术2025-06-21  41

    内容: 记录求二叉树的镜像的方法

    思路:

    求解二叉树的镜像也就是递归对子树进行求解,每次将一个节点的左右子树交换,然后对其子节点执行 同样的左右子树交换

    代码:

    typedef struct node { char c; struct node * left; struct node * right; }node,*node_ptr; node_ptr tree_Mirror(node_ptr root) { if (!root || (root->left == NULL && root->right == NULL)) { return root; } node_ptr tmp = root->left; root->left = root->right; root->right = tmp; if (!root->left) { tree_Mirror(root->left); } if (!root->right) { tree_Mirror(root->right); } return root; }
    Processed: 0.010, SQL: 12