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 ↩︎

上一篇:使用 docker 部署 elasticsearch 并安装 ik 中文分词器


下一篇:Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: Access denied for user