博客
关于我
剑指offer(牛客)---18.二叉树的镜像
阅读量:745 次
发布时间:2019-03-17

本文共 752 字,大约阅读时间需要 2 分钟。

题目描述

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

输入描述:

二叉树的镜像定义:源二叉树     	    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) {        TreeNode temp=null;        if(root!=null) {        	temp=root.left;        	root.left=root.right;        	root.right=temp;        	if(root.left!=null) {        		Mirror(root.left);        	}        	if(root.right!=null) {        		Mirror(root.right);        	}        }    }}

这题又是考察递归;

解题思路:先判断根结点,如果不为空就将它的左右两个孩子交互,然后递归,分别进入当前交换后的左孩子和右孩子继续交换;

转载地址:http://mkbhz.baihongyu.com/

你可能感兴趣的文章
Netty源码—4.客户端接入流程一
查看>>
Netty源码—4.客户端接入流程二
查看>>
Netty源码—5.Pipeline和Handler一
查看>>
Netty源码—5.Pipeline和Handler二
查看>>
Netty源码—6.ByteBuf原理一
查看>>
Netty源码—6.ByteBuf原理二
查看>>
Netty源码—7.ByteBuf原理三
查看>>
Netty源码—7.ByteBuf原理四
查看>>
Netty源码—8.编解码原理一
查看>>
Netty源码—8.编解码原理二
查看>>
Netty源码解读
查看>>
Netty的Socket编程详解-搭建服务端与客户端并进行数据传输
查看>>
Netty相关
查看>>
Netty简介
查看>>
Netty遇到TCP发送缓冲区满了 写半包操作该如何处理
查看>>
Netty:ChannelPipeline和ChannelHandler为什么会鬼混在一起?
查看>>
Netty:原理架构解析
查看>>
Network Dissection:Quantifying Interpretability of Deep Visual Representations(深层视觉表征的量化解释)
查看>>
Network Sniffer and Connection Analyzer
查看>>
Network 灰鸽宝典【目录】
查看>>