题目描述

操作给定的二叉树,将其变换为源二叉树的镜像。
 

输入描述:

二叉树的镜像定义:源二叉树 
    	    8
    	   /  \
    	  6   10
    	 / \  / \
    	5  7 9 11
    	镜像二叉树
    	    8
    	   /  \
    	  10   6
    	 / \  / \
    	11 9 7  5

 题目链接:

https://www.nowcoder.com/practice/564f4c26aa584921bc75623e48ca3011?tpId=13&tqId=11171&rp=1&ru=/activity/oj&qru=/ta/coding-interviews/question-ranking

 

 

分析:

左右子树全部交换一次即可。注意终止条件。

 

 

 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         TreeNode tmp = root.left;
20         root.left = root.right;
21         root.right = tmp;
22         Mirror(root.left);
23         Mirror(root.right);
24     }
25 }

 

 
 
 
 
 
 
 
 
 
 





posted on 2020-06-06 14:52  MoonBeautiful  阅读(127)  评论(0编辑  收藏  举报