剑指offer27.二叉树的镜像 P157

mac2025-07-30  3

剑指offer27.二叉树的镜像 P157

题目:请完成一个函数,输入一个二叉树,该函数输出它的镜像。

// 其实就是先根遍历的模板,在左右子树递归访问前交换左右子树

void MirrorRecursively(BinaryTreeNode *pNode) { if (pNode == NULL) return; // 两个递归出口 if (pNode -> left == NULL && pNode -> right == NULL) return ; BinaryTreeNode *temp = pNode -> left; // 交换左右子树(先根遍历是访问节点) pNode -> left = pNode -> right; pNode -> right = temp; if (pNode -> left) // 左递归 MirrorRecursively(pNode -> left); if (pNode -> right) // 右递归 MirrorRecursively(pNode -> right); }
最新回复(0)