操作给定的二叉树,将其变换为源二叉树的镜像。
思路:
使用递归,进行逐个交换。
1 /** 2 public class TreeNode { 3 int val = 0; 4 TreeNode left = null; 5 TreeNode right = null; 6 7 public TreeNode(int val) { 8 this.val = val; 9 10 } 11 12 } 13 */ 14 public class Solution { 15 public void Mirror(TreeNode root) { 16 TreeNode tmp = null; 17 if (root != null){ 18 tmp = root.left; 19 root.left = root.right; 20 root.right = tmp; 21 if(root.left != null){ 22 Mirror(root.left); 23 } 24 if(root.right != null){ 25 Mirror(root.right); 26 } 27 } 28 } 29 }
原文:http://www.cnblogs.com/LoganChen/p/6398549.html