剑指offer-(16)二叉树的镜像

时间:2017-10-23 21:13:10   收藏:0   阅读:199

题目描述

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

输入描述:

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

技术分享

题目分析

 很简单,交换左右节点,递归

代码

/**
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;
        TreeNode tempNode=null;
        Mirror(root.left);
        Mirror(root.right);
        tempNode=root.right;
        root.right=root.left;
        root.left=tempNode;
    }
}

运行结果

技术分享

 

原文:http://www.cnblogs.com/wuguanglin/p/Mirror.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!