码迷,mamicode.com
首页 > 其他好文 > 详细

[剑指offer]二叉树的镜像

时间:2019-08-29 13:57:51      阅读:101      评论:0      收藏:0      [点我收藏+]

标签:tps   http   ice   coding   pre   style   interview   nod   ret   

题目描述

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

输入描述:

二叉树的镜像定义:源二叉树 
    	    8
    	   /      	  6   10
    	 / \  /     	5  7 9 11
    	镜像二叉树
    	    8
    	   /      	  10   6
    	 / \  /     	11 9 7  5





题目链接:
https://www.nowcoder.com/practice/564f4c26aa584921bc75623e48ca3011?tpId=13&tqId=11171&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking



package com.sunshine.OFFER66_SECOND;

public class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode(int x) {
        val = x;
    }
}

 





package com.sunshine.OFFER66_SECOND;

import org.junit.Test;

public class A18_Mirror {

    @Test
    public void test(){
        TreeNode n1 =new TreeNode(1);
        TreeNode n2 =new TreeNode(2);
        TreeNode n3 =new TreeNode(3);
        TreeNode n4 =new TreeNode(4);
        TreeNode n5 =new TreeNode(5);
        TreeNode n6 =new TreeNode(6);
        TreeNode n7 =new TreeNode(7);

        n1.left=n2;
        n1.right=n3;
        n2.left=n4;
        n2.right=n5;
        n3.left=n6;
        n3.right=n7;

        Mirror(n1);
        TreeUtility.printTreeOfLine(n1);
    }


    public void Mirror(TreeNode root) {
        if(root == null ){
            return;
        }
        TreeNode right = root.right;
        root.right = root.left;
        root.left = right;
        Mirror(root.left);
        Mirror(root.right);
    }
}

 

[剑指offer]二叉树的镜像

标签:tps   http   ice   coding   pre   style   interview   nod   ret   

原文地址:https://www.cnblogs.com/MoonBeautiful/p/11428879.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!