Title: Sword finger Offer 27. Binary tree mirror

Complete a function that inputs a binary tree and outputs its mirror image.

For example, enter:

4/2 7 / / 1 3 6 9 Mirror output:

4/7 2 / \ / 9 6 3 1

 

Example 1:

Input: root = [4,2,7,1,3,6,9]

Limitations:

0 <= Number of nodes <= 1000

Solution: the use of recursive traversal, the idea is the same as the tree before the order traversal, first exchange (before the order traversal is the first output node content), and then the left and right sub-tree recursive cycle traversal

public TreeNode mirrorTree(TreeNode root) { if (root ! TreeNode node = root.left; TreeNode node = root.left; root.left = root.right; root.right = node; // Loop recursion left subtree mirrorTree(root.left); // Loop recursion right subtree mirrorTree(root.right); } return root; }Copy the code