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 / \ / \ ...
随机推荐
- Linux 编译C++ 与 设置 Vim
1. Linux 下编译c++ vim test.cpp // 创建文件 g++ test.cpp // 编译文件 ./a.out // 执行文件 g++ test.cpp ...
- ionic开发android App
在win下配置ionic可以参考七月的这篇博客:http://www.cnblogs.com/shikelong/p/4480975.html. 依照七月的思路基本可以创建一个ionic项目了,下面我 ...
- sql%found sql%notfound sql%rowcount sql%isopen
原文引入:http://blog.csdn.net/mh942408056/article/details/6949325 sql%found sql%notfound sql%rowcount sq ...
- 【Learning】多项式的一些东西
FFT 坑 NTT 将\(FFT\)中的单位复数根改成原根即可. 卡常版NTT模版 struct Mul { int Len; int wn[N], Lim; int rev[N]; inline v ...
- tyvj 2054 [Nescafé29]四叶草魔杖【克鲁斯卡尔+状压dp】
传送:http://www.joyoi.cn/problem/tyvj-2054 来自lyd课件: 所以先预处理出各个sum为0的块,然后状压dfs取min来得到答案 #include<iost ...
- P3953 逛公园(dp,最短路)
P3953 逛公园 题目描述 策策同学特别喜欢逛公园.公园可以看成一张NN个点MM条边构成的有向图,且没有 自环和重边.其中1号点是公园的入口,NN号点是公园的出口,每条边有一个非负权值, 代表策策经 ...
- 11.7NOIP模拟题
/* 有循环节 */ #include<cstdio> #include<cstring> #include<iostream> #include<algor ...
- wamp集成环境下帝国备份出错
我在本地wamp环境下面使用帝国备份王时,报错信息如下: Parse error: syntaxerror, unexpected $end in D:wampwwwhuifuclassfunctio ...
- [Swift]Set(集)转换为Array(数组)
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- JavaScript--编程练习2
制作一个跳转提示页面: 要求: 1. 如果打开该页面后,如果不做任何操作则5秒后自动跳转到一个新的地址,如慕课网主页. 2. 如果点击“返回”按钮则返回前一个页面. 效果: 注意: 在窗口中运行该程序 ...