剑指offer——二叉树的镜像

题目链接:操作给定的二叉树,将其变换为源二叉树的镜像。

解题思路:应用递归,先交换左子树与右子树,然后再交换左子树的子节点和右子树的子节点。

 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 import java.util.Stack;
15 public class Solution {
16     public void Mirror(TreeNode root) {
17         
18         if(root==null)
19             return ;
20         if(root.left==null && root.right==null)
21             return ;
22         TreeNode temp = root.left;
23         root.left=root.right;
24         root.right= temp;
25         
26         if(root.left!=null)
27             Mirror(root.left);
28         if(root.right!=null)
29             Mirror(root.right);
30             
31         
32     }
33 }
原文地址:https://www.cnblogs.com/wangyufeiaichiyu/p/10860649.html