美文网首页剑指 Offer Java版
剑指Offer Java版 面试题27:二叉树的镜像

剑指Offer Java版 面试题27:二叉树的镜像

作者: 孙强Jimmy | 来源:发表于2019-07-19 20:16 被阅读377次

    题目:操作给定的二叉树,将其变换为源二叉树的镜像。

    练习地址

    https://www.nowcoder.com/practice/564f4c26aa584921bc75623e48ca3011

    参考答案

    /**
    public class TreeNode {
        int val = 0;
        TreeNode left = null;
        TreeNode right = null;
    
        public TreeNode(int val) {
            this.val = val;
        }
    }
    */
    public class Solution {
        public void Mirror(TreeNode root) {
            if (root == null) {
                return;
            }
            if (root.left == null && root.right == null) {
                return;
            }
            TreeNode temp = root.left;
            root.left = root.right;
            root.right = temp;
            Mirror(root.left);
            Mirror(root.right);
        }
    }
    

    复杂度分析

    • 时间复杂度:O(n)。
    • 空间复杂度:O(logn)。

    👉剑指Offer Java版目录
    👉剑指Offer Java版专题

    相关文章

      网友评论

        本文标题:剑指Offer Java版 面试题27:二叉树的镜像

        本文链接:https://www.haomeiwen.com/subject/enqelctx.html