博客
关于我
剑指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/

你可能感兴趣的文章
nacos集群配置详解
查看>>
Nagios 3.0 Jumpstart Guide For Linux – Overview, Installation and Configuration
查看>>
nagios 实时监控 iptables 状态
查看>>
WAP短信格式解析及在Linux下用C语言实现
查看>>
nagios+cacti整合
查看>>
Nagios介绍
查看>>
nagios利用NSCient监控远程window主机
查看>>
nagios安装文档
查看>>
nagios服务端安装
查看>>
Nagios自定义监控脚本
查看>>
name_save matlab
查看>>
Nami 项目使用教程
查看>>
Nancy之基于Nancy.Hosting.Aspnet的小Demo
查看>>
NAND NOR FLASH闪存产品概述
查看>>
nano 编辑
查看>>
nanoGPT 教程:从零开始训练语言模型
查看>>
NASA网站曝严重漏洞,或将沦为黑客钓鱼网站?
查看>>
Nash:轻量级、安全且可靠的脚本语言
查看>>
NAS、SAN和DAS的区别
查看>>
NAS个人云存储服务器搭建
查看>>