4
头图

Mirror of Binary Tree

Title description

Operate the given binary tree and transform it into a mirror image of the source binary tree.

title link : Mirror image of binary tree

Code

/**
 * 标题:二叉树的镜像
 * 题目描述
 * 操作给定的二叉树,将其变换为源二叉树的镜像。
 * 题目链接:
 * https://www.nowcoder.com/practice/564f4c26aa584921bc75623e48ca3011?tpId=13&&tqId=11171&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
 */
public class Jz18 {

    /**
     * 递归法
     *
     * @param root
     */
    public static void mirror(TreeNode root) {
        if (root == null || (root.left == null && root.right == null)) {
            return;
        }
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
        mirror(root.left);
        mirror(root.right);
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        mirror(root);
    }
}
[Daily Message] The most dazzling light in the world is the appearance of your hard work except the sun.

醉舞经阁
1.8k 声望7.1k 粉丝

玉树临风,仙姿佚貌!