二叉树的镜像
二叉树的镜像_牛客网
https://www.nowcoder.com/practice/564f4c26aa584921bc75623e48ca3011?tpId=13&tqId=11171&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
方法有二:
其一:先序遍历,第一次经过结点时交换左右孩子
void Mirror(TreeNode *pRoot) {
if(!pRoot) return;
swap(pRoot->left,pRoot->right);
Mirror(pRoot->left);
Mirror(pRoot->right);
}
其二:后序遍历,最后一次经过结点时交换左右孩子
void Mirror(TreeNode *pRoot) {
if(!pRoot) return;
Mirror(pRoot->left);
Mirror(pRoot->right);
swap(pRoot->left,pRoot->right);
}
腾讯云智研发成长空间 294人发布