To prove safety: binary tree mirror

Title Description

Enter a binary tree, it is converted into its mirror image.

Input Tree:
       . 8 
     / \
     . 6 10 
   / \ / \
   . 5. 7. 9. 11 

 [ 8,6,10,5,7,9,11, null , null , null , null , null , null , null , null ] 
output tree:
       . 8 
     / \
     10. 6 
   / \ / \
   . 11. 9. 7. 5 

 [ 8,10,6,11,9,7,5, null , null , null , null , null , null , null,null]

 

 

solution

The root of the exchange about children, about children after recursive.

 

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public void mirror(TreeNode root) {
        if (root == null || (root.left == null && root.right == null)) {
            return;
        }
        TreeNode t = root.left;
        root.left = root.right;
        root.right = t;
        mirror(root.left);
        mirror(root.right);
    }
}

 

Guess you like

Origin www.cnblogs.com/lisen10/p/11183577.html