leetcode刷题-剑指offer-27题

题目[1]

请完成一个函数,输入一个二叉树,该函数输出它的镜像。

例如输入:

4
/
2 7
/ \ /
1 3 6 9

镜像输出:

4
/
7 2
/ \ /
9 6 3 1

示例 1:

输入:root = [4,2,7,1,3,6,9]
输出:[4,7,2,9,6,3,1]

解答

新手上路,才学疏浅,望斧正

public class Solution7_2 {

    public TreeNode mirrorTree(TreeNode root) {
        if(root==null){
            return null;
        }
        mirror(root);
        return  root;
    }


    public void mirror(TreeNode node){
        if(node==null) {
            return;
        }

        TreeNode tmpLeft=node.left;
        node.left=node.right;
        node.right=tmpLeft;
        mirror(node.left);
        mirror(node.right);

    }

}

  1. 力扣(LeetCode)https://leetcode-cn.com/problems/er-cha-shu-de-jing-xiang-lcof ↩︎

posted @ 2022-01-13 13:23  发呆鱼  阅读(63)  评论(0)    收藏  举报