N18_二叉树的镜像
题目描述
输入描述:
二叉树的镜像定义:源二叉树
8
/ \
6 10
/ \ / \
5 7 9 11
镜像二叉树
8
/ \
10 6
/ \ / \
11 9 7 5
package new_offer;
/**
* 操作给定的二叉树,将其变换为源二叉树的镜像。
* @author Sonya
*思路:递归调用
*/
public class N18_Mirror {
public void Mirror(TreeNode root) {
//TreeNode temp;
if(root!=null) {
TreeNode temp=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);}
}
}
public void print(TreeNode root) {
System.out.print(root.val);
if(root.left!=null) {print(root.left);}
if(root.right!=null){print(root.right);}
} public static void main(String[] args) {
// TODO Auto-generated method stub
TreeNode t1,t2,t3,t4,t5,t6;
t1=new TreeNode(1);t2=new TreeNode(2);t3=new TreeNode(3);
t4=new TreeNode(4);t5=new TreeNode(5);t6=new TreeNode(6);
t1.left=t2;t1.right=t3;
t2.left=t4;t2.right=t5;
t3.left=t3.right=null;
t4.left=t6;t4.right=null;
t5.left=t5.right=null;
t6.left=t6.right=null;
N18_Mirror n18=new N18_Mirror();
n18.print(t1);
System.out.println(" ");
n18.Mirror(t1);
n18.print(t1);
} }
N18_二叉树的镜像的更多相关文章
- 剑指Offer面试题:18.二叉树的镜像
一.题目:二叉树的镜像 题目:请完成一个函数,输入一个二叉树,该函数输出它的镜像.例如下图所示,左图是原二叉树,而右图则是该二叉树的镜像. 该二叉树节点的定义如下,采用C#语言描述: public c ...
- 剑指Offer 二叉树的镜像
题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述: 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ ...
- 剑指Offer:面试题19——二叉树的镜像(java实现)
问题描述: 操作给定的二叉树,将其变换为源二叉树的镜像. 二叉树结点定义为: public class TreeNode { int val = 0; TreeNode left = null; Tr ...
- (剑指Offer)面试题19:二叉树的镜像
题目: 操作给定的二叉树,将其变换为源二叉树的镜像. 二叉树的定义如下: struct TreeNode{ int val; TreeNode* left; TreeNode* right; }; 输 ...
- 《剑指offer》— JavaScript(18)二叉树的镜像
二叉树的镜像 题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 相关知识 二叉树的镜像定义: 源二叉树 镜像二叉树 思路 有关二叉树的算法问题,一般都可以通过递归来解决.那么写一个正确的递归程序 ...
- 剑指offer——二叉树的镜像
题目:操作给定的二叉树,将其变换为源二叉树的镜像. 思路:前序(根左右的顺序)遍历一棵树,在存储的时候将其左右树进行交换,最后按照处理后的树还原,即得到其镜像. /** public class Tr ...
- 包含min函数的栈 ,二叉树的镜像
包含min函数的栈 问题 定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1)). 代码 # -*- coding:utf-8 -*- class Sol ...
- 《剑指offer》 二叉树的镜像
本题来自<剑指offer>二叉树的镜像 题目: 操作给定的二叉树,将其变换为源二叉树的镜像. 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 ...
- 剑指offer(18)二叉树的镜像
题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述: 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ ...
随机推荐
- “指定的SAS安装数据(sid)文件不能用于选定的SAS软件订单
Sas安装指南,若拿到可用的sid文件,需要做如下修改. 使用新sid的时候,“指定的SAS安装数据(sid)文件不能用于选定的SAS软件订单”问题解决: 1.进入sas安装包,install_doc ...
- mybtis 逆向
mbg.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfi ...
- HDU 5752Sqrt Bo
Sqrt Bo Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total S ...
- YTU 2697: 血型统计
2697: 血型统计 时间限制: 1 Sec 内存限制: 128 MB 提交: 405 解决: 164 题目描述 黑猫警长在犯罪现场发现了一些血迹,现已经委托检验机构确定了血型,需要统计各种血型的 ...
- 【Codeforces】665E Beautiful Subarrays
E. Beautiful Subarrays time limit per test: 3 seconds memory limit per test: 512 megabytes input: st ...
- Python split 分割中文
str8="中国 和 韩国 的区别" # a=str8.find("Python") # print a b=str8.find("和") ...
- 10.19NOIP模拟赛(DAY2)
/* 正解O(n)尺取法orz 我写的二分答案.本来以为会被卡成暴力分...... 这个-'0'-48是我写的吗........我怎么不记得... */ #include<bits\stdc++ ...
- Invalid default value for 'create_date' timestamp field
创建表的语句中有这么一句 `create_date` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00', 1 之后就报了这个错误. That is be ...
- C# 从服务器下载文件并保存到客户端
参考代码: using System; using System.Net; namespace HT.SIHONG.Common.Utility { public class DownloadFile ...
- connection failed to http://nssa-sensor3:11000/oozie/?user.name=oozie(<urlopen erroer Errno 111] Connection refused>)解决办法(图文详解)
不多说,直接上干货! 解决办法 Copy/Paste oozie.services property tag set from oozie-default.xml to oozie-site.xml. ...