二种方法:STACK和递归调用
二叉树的镜像
http://www.nowcoder.com/questionTerminal/564f4c26aa584921bc75623e48ca3011
碰到节点就swap(left,right)就完事了,然后在选择一种遍历方法 入栈的或者递归的:前中后序遍历方法。参考:二叉树:层序,前,中,后 序遍历的解题模板https://blog.csdn.net/sinat_36338365/article/details/107916628
class Solution {
public:
void Mirror(TreeNode *pRoot) {
if(pRoot==NULL)return ;//如果节点为空则返回
TreeNode *p=pRoot;
//方法一:stack
/*stack<TreeNode*>sta;//前序(中后都行)遍历每个节点,然后交换他们的左右节点
sta.push(p);
while(!sta.empty()){
TreeNode *temp=sta.top();
sta.pop();
swap(temp->left,temp->right);//将节点的左右子树进行交换
if(temp->left) sta.push(temp->left);
if(temp->right) sta.push(temp->right);
}*/
//方法二:递归调用,每次都把根节点的左右子树交换 知道节点为NULL
swap(p->left,p->right);
Mirror( p->left);
Mirror( p->right);
}
};