二叉树的镜像

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

输入描述:

二叉树的镜像定义:源二叉树 
    	    8
    	   /  
    	  6   10
    	 /   / 
    	5  7 9 11
    	镜像二叉树
    	    8
    	   /  
    	  10   6
    	 /   / 
    	11 9 7  5
 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){}else{
17             TreeNode temp = null;
18         if(root.left!=null||root.right!=null){
19             temp = root.left;
20             root.left = root.right;
21             root.right = temp;
22             if(root.left!=null){
23                 Mirror(root.left);
24             }
25             if(root.right!=null){
26                 Mirror(root.right);
27             }
28         }
29        }
30     }
31 }

只有左右子树不全为空就交换。

原文地址:https://www.cnblogs.com/haq123/p/12178658.html