其他分享
首页 > 其他分享> > 二叉树的镜像——JZ18

二叉树的镜像——JZ18

作者:互联网

https://www.nowcoder.com/practice/a9d0ecbacef9410ca97463e4a5c83be7?tpId=13&tags=&title=&difficulty=0&judgeStatus=0&rp=1

 

描述

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

比如:    源二叉树 
            8
           /  \
          6   10
         / \  / \
        5  7 9 11
        镜像二叉树
            8
           /  \
          10   6
         / \  / \
        11 9 7  5

示例1

输入:

{8,6,10,5,7,9,11}

复制返回值:

{8,10,6,11,9,7,5}

 

public class Jz18_Mirror {


    public TreeNode Mirror(TreeNode pRoot) {
        if(null == pRoot){
            return null;
        }
        TreeNode t = pRoot.left;
        pRoot.left =pRoot.right;
        pRoot.right = t;
        Mirror(pRoot.left);
        Mirror(pRoot.right);
        return pRoot;
    }


    public class TreeNode {
        int val = 0;
        TreeNode left = null;
        TreeNode right = null;

        public TreeNode(int val) {
            this.val = val;
        }
    }

}

 

标签:right,TreeNode,JZ18,pRoot,二叉树,Mirror,镜像,null
来源: https://blog.csdn.net/sunshine_YG/article/details/117416288