操作给定的二叉树,将其变换为源二叉树的镜像。
输入描述:
二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ 11 9 7 5
方法一:C++
class Solution {
public:
void Mirror(TreeNode *root) {
if(!root) return;
Mirror(root -> left);
Mirror(root -> right);
//交换左右结点
swap(root -> left,root -> right);
}
};
方法二:
public class Solution {
public void Mirror(TreeNode root) {
//递归结束条件
if (root == null)
return;
//业务处理
TreeNode temp = root.left;
root.left = root.right;
root.right = temp;
Mirror(root.left);
Mirror(root.right);
}
}