题目描述
操作给定的二叉树,将其变换为源二叉树的镜像。
输入描述:
二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ 11 9 7 5
思路:递归 递归函数返回的是子节点交换后的节点;
public class Solution {
public void Mirror(TreeNode root) {
root = help(root);
}
private TreeNode help(TreeNode root){
if(root == null){
return null;
}
TreeNode left = help(root.left);
TreeNode right = help(root.right);
root.left = right;
root.right = left;
return root;
}
}
public class Solution {
public void Mirror(TreeNode root) {
root = help(root);
}
private TreeNode help(TreeNode root){
if(root == null){
return null;
}
TreeNode t = help(root.left);
root.left = help(root.right);
root.right = t;
return root;
}
}