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 / \ / \ ...
随机推荐
- SpringMVC导出Excel
import java.math.BigDecimal; import java.net.URLEncoder; import java.text.SimpleDateFormat; import j ...
- 20160225.CCPP体系具体解释(0035天)
程序片段(01):CircleList.h+CircleList.c+main.c 内容概要:环形链表 ///CircleList.h #pragma once #include <stdio. ...
- mouse_event function
https://msdn.microsoft.com/en-us/library/windows/desktop/ms646260(v=vs.85).aspx
- luogu1966 火柴排队
题目大意 涵涵有两盒火柴,每盒装有 n 根火柴,每根火柴都有一个高度.现在将每盒中的火柴各自排成一列,同一列火柴的高度互不相同,两列火柴之间的距离定义为: $\sum_{i=1}^n(a_i-b_i) ...
- android studio 使用
java5-7适用android,java8对安卓支持不好. mac osx 需要安装jdk8, google Nexus模拟器, Intellij是JetBrains发布的. Intellij有2个 ...
- 8-13 canvas专题-阶段练习二(下)
8-13 canvas专题-阶段练习二(下) <!DOCTYPE html> <html lang="zh-cn"> <head> <me ...
- 控件CListCtr详解
1.CListCtrl控件 CListCtrl控件在数据库编程中是用得比较多的控件之一,也是Window控件中较难掌握的一个控件.他可以有四显示方式,Report.List.Icon.SmallIco ...
- 8 Range 对象
8.1 引用Range 引用Range的主要方法: Application.ActiveCell Application.Range Application.Selection Worksheet.C ...
- bzoj1776
点分治/贪心 对于点分治的理解不够深刻...点分治能统计树上每个点对的信息,那么这里就是统计同种颜色点对之间的最大距离,自然可以用点分 然后点分,每次统计最大距离,但是略微卡常... 还有一种贪心的方 ...
- Python基础 — OS
OS模块 -- 简介 OS模块是Python标准库中的一个用于访问操作系统功能的模块,OS模块提供了一种可移植的方法使用操作系统的功能.使用OS模块中提供的接口,可以实现跨平台访问.但是在OS模块 ...