题目
《剑指offer-面试题19》P125
请完成一个函数,输入一个二叉树,该函数输出它的镜像。
二叉树定义如下:
struct BinaryTreeNode
{
int m_nValue;
BinaryTreeNode* m_pLeft;
BinaryTreeNode* m_pRight;
}
想法
比较简单的是:
输出的可以是原来的树,所以呢,我们不用对树进行拷贝操作。其实就是将左右子树兑换。
迭代方法
public void MirrorRecursively(TreeNode root){
if(root == null||(root.left ==null&& root.right == null))
return ;
TreeNode temp = root.right;
root.right = root. left;
root.left = temp;
if(root.left!=null)
MirrorRecursively(root.left);
if(root.right!=null)
MirrorRecursively(root.right);
}