2

JZ-018-二叉树的镜像

 2 years ago
source link: https://segmentfault.com/a/1190000041077044
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

JZ-018-二叉树的镜像

发布于 12 月 7 日

二叉树的镜像

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

题目链接: 二叉树的镜像

/**
 * 标题:二叉树的镜像
 * 题目描述
 * 操作给定的二叉树,将其变换为源二叉树的镜像。
 * 题目链接:
 * 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);
    }
}

【每日寄语】 世上最耀眼的光芒除了太阳还有你努力的模样。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK