(21) into a binary tree, so that mirror-symmetrical

I. Description of the problem

Given a binary tree, binary conversion, binary tree so that the original mirror-symmetrical

 

Two, Code

 1 package algorithm;
 2 
 3 /**
 4  * Created by adrian.wu on 2019/5/29.
 5  */
 6 public class MirrowRecursive {
 7     public static class TreeNode {
 8         TreeNode left;
 9         TreeNode right;
10         int val;
11 
12         public TreeNode(int val) {
13             this.val = val;
14         }
15     }
16 
17     public static void mirrowRecursive(TreeNode head) {
18         if (head == null) return;
19         if (head.left == null && head.right == null) return;
20 
21         TreeNode leftTemp = head.left;
22         head.left = head.right;
23         head.right = leftTemp;
24 
25         if (head.left != null) mirrowRecursive(head.left);
26         if (head.right != null) mirrowRecursive(head.right);
27     }
28 }

 

Guess you like

Origin www.cnblogs.com/ylxn/p/10949119.html