(十八)

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

                                                                

 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         if(root == null){
17             return;
18         }
19         if(root.left == null && root.right == null){
20             return;
21         }
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         }
29         
30         if(root.right!=null){
31             Mirror(root.right);
32         }
33         
34     }
35 }

 

posted @ 2018-12-13 18:21  落水河图  阅读(114)  评论(0)    收藏  举报