The sword refers to the offer 27. The mirror image of the binary tree

The sword refers to the offer 27. The mirror image of the binary tree

Title description

Insert picture description here

Problem-solving ideas

The main is the exchange root.leftand root.rightadvance with temp save one pointer on the line.

class Solution {
    
    
    //将以root为根节点的树镜像翻转,返回翻转后的根节点
    public TreeNode mirrorTree(TreeNode root) {
    
    
        //base case
        if (root == null) return null;

        TreeNode temp = mirrorTree(root.left);
        root.left = mirrorTree(root.right);
        root.right = temp;
        
        return root;
    }
}

Guess you like

Origin blog.csdn.net/cys975900334/article/details/115048984