递归实现二叉树镜像
假设我们的函数一定能实现这样的功能:
1、对传入的根节点对应的二叉树镜像,返回镜像后的根节点
2、终止条件:当输入为空节点的时候,返回空节点
TreeNode* Mirror(TreeNode* pRoot) {
// write code here
if(pRoot==nullptr){
return pRoot;
}
TreeNode* tmp_right = Mirror(pRoot->right);
TreeNode* tmp_left = Mirror(pRoot->left);
// auto tmp = tmp_left;
// tmp_left = tmp_right;
// tmp_right = tmp;
pRoot->left = tmp_right;
pRoot->right = tmp_left;
return pRoot;
}

京公网安备 11010502036488号