P125、面试题19:二叉树的镜像
题目:请完成一个函数,输入一个二叉树,该函数输出它的镜像
二叉树结点的定义如下:
struct BinaryTreeNode{
int m_nValue;
BinaryTreeNode* m_pLeft;
BinaryTreeNode* m_pRight;
}
package com.yyq; /**
* Created by Administrator on 2015/9/15.
*/
public class MirrorOfBinaryTree {
public static BinaryTreeNode mirrorOfBinaryTree(BinaryTreeNode pNode) {
if (pNode == null)
return null;
if (pNode.getM_pLeft() == null && pNode.getM_pRight() == null)
return null;
BinaryTreeNode pTemp = pNode.getM_pLeft();
pNode.setM_pLeft(pNode.getM_pRight());
pNode.setM_pRight(pTemp);
if (pNode.getM_pLeft() != null)
mirrorOfBinaryTree(pNode.getM_pLeft());
if (pNode.getM_pRight() != null)
mirrorOfBinaryTree(pNode.getM_pRight());
return pNode;
} //先序遍历
public static void preprintTree(BinaryTreeNode pRoot) {
if (pRoot == null)
return;
System.out.print(pRoot.getM_nValue() + "\t");
preprintTree(pRoot.getM_pLeft());
preprintTree(pRoot.getM_pRight());
} //中序遍历
public static void midprintTree(BinaryTreeNode pRoot) {
if (pRoot != null) {
midprintTree(pRoot.getM_pLeft());
System.out.print(pRoot.getM_nValue() + "\t");
midprintTree(pRoot.getM_pRight());
}
} public static void Test(String testName, BinaryTreeNode pRoot){
if (testName == null)
return ;
System.out.println("previous printing tree is:");
preprintTree(pRoot);
System.out.println();
System.out.println("middle printing tree is:");
midprintTree(pRoot); System.out.println("\n====="+testName+": MirrorRecursively=====");
BinaryTreeNode pNode1 = mirrorOfBinaryTree(pRoot);
System.out.println("previous printing tree is:");
preprintTree(pNode1);
System.out.println();
System.out.println("middle printing tree is:");
midprintTree(pNode1); System.out.println("\n====="+testName+": MirrorIteratively=====");
BinaryTreeNode pNode2 = mirrorOfBinaryTree(pRoot);
System.out.println("previous printing tree is:");
preprintTree(pNode2);
System.out.println();
System.out.println("middle printing tree is:");
midprintTree(pNode2);
System.out.println();
} // ====================测试代码====================
// 测试完全二叉树:除了叶子节点,其他节点都有两个子节点
// 8
// 6 10
// 5 7 9 11
public static void Test1() {
System.out.println("\n=====Test1 starts:=====");
BinaryTreeNode pNode8 = new BinaryTreeNode(8);
BinaryTreeNode pNode6 = new BinaryTreeNode(6);
BinaryTreeNode pNode10 = new BinaryTreeNode(10);
BinaryTreeNode pNode5 = new BinaryTreeNode(5);
BinaryTreeNode pNode7 = new BinaryTreeNode(7);
BinaryTreeNode pNode9 = new BinaryTreeNode(9);
BinaryTreeNode pNode11 = new BinaryTreeNode(11); pNode8.connectTreeNodes(pNode6, pNode10);
pNode6.connectTreeNodes(pNode5, pNode7);
pNode10.connectTreeNodes(pNode9, pNode11); Test("Test1",pNode8);
pNode8 = null;
} // 测试二叉树:出叶子结点之外,左右的结点都有且只有一个左子结点
// 8
// 7
// 6
// 5
//
public static void Test2() {
System.out.println("\n=====Test2 starts:=====");
BinaryTreeNode pNode8 = new BinaryTreeNode(8);
BinaryTreeNode pNode7 = new BinaryTreeNode(7);
BinaryTreeNode pNode6 = new BinaryTreeNode(6);
BinaryTreeNode pNode5 = new BinaryTreeNode(5);
BinaryTreeNode pNode4 = new BinaryTreeNode(4); pNode8.connectTreeNodes(pNode7, null);
pNode7.connectTreeNodes(pNode6, null);
pNode6.connectTreeNodes(pNode5, null);
pNode5.connectTreeNodes(pNode4, null); Test("Test2",pNode8);
pNode8 = null;
} // 测试二叉树:出叶子结点之外,左右的结点都有且只有一个右子结点
// 8
// 7
// 6
// 5
// 4
public static void Test3() {
System.out.println("\n=====Test3 starts:=====");
BinaryTreeNode pNode8 = new BinaryTreeNode(8);
BinaryTreeNode pNode7 = new BinaryTreeNode(7);
BinaryTreeNode pNode6 = new BinaryTreeNode(6);
BinaryTreeNode pNode5 = new BinaryTreeNode(5);
BinaryTreeNode pNode4 = new BinaryTreeNode(4); pNode8.connectTreeNodes(null, pNode7);
pNode7.connectTreeNodes(null, pNode6);
pNode6.connectTreeNodes(null, pNode5);
pNode5.connectTreeNodes(null, pNode4); Test("Test3",pNode8);
pNode8 = null;
} // 测试空二叉树:根结点为空指针
public static void Test4() {
System.out.println("\n=====Test4 starts:=====");
BinaryTreeNode pNode = null; Test("Test4",pNode);
} // 测试只有一个结点的二叉树
public static void Test5() {
System.out.println("\n=====Test5 starts:=====");
BinaryTreeNode pNode8 = new BinaryTreeNode(8); Test("Test5",pNode8);
pNode8 = null;
} public static void main(String[] args) {
Test1();
Test2();
Test3();
Test4();
Test5();
}
}
P125、面试题19:二叉树的镜像的更多相关文章
- 剑指Offer:面试题19——二叉树的镜像(java实现)
问题描述: 操作给定的二叉树,将其变换为源二叉树的镜像. 二叉树结点定义为: public class TreeNode { int val = 0; TreeNode left = null; Tr ...
- 剑指offer面试题19 二叉树的镜像
题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ ...
- 《剑指offer》面试题19 二叉树的镜像 Java版
书中方法:这道题目可能拿到手没有思路,我们可以在纸上画出简单的二叉树来找到规律.最后我们发现,镜像的实质是对于二叉树的所有节点,交换其左右子节点.搞清楚获得镜像的方法,这道题实际上就变成了一道二叉树遍 ...
- 【剑指Offer】面试题27. 二叉树的镜像
题目 请完成一个函数,输入一个二叉树,该函数输出它的镜像. 例如输入: 4 / \ 2 7 / \ / \ 1 3 6 9 镜像输出: 4 ...
- 《剑指offer》面试题27. 二叉树的镜像
问题描述 请完成一个函数,输入一个二叉树,该函数输出它的镜像. 例如输入: 4 / \ 2 7 / \ / \ 1 3 6 9 镜像输出: 4 ...
- (剑指Offer)面试题19:二叉树的镜像
题目: 操作给定的二叉树,将其变换为源二叉树的镜像. 二叉树的定义如下: struct TreeNode{ int val; TreeNode* left; TreeNode* right; }; 输 ...
- 剑指Offer面试题:18.二叉树的镜像
一.题目:二叉树的镜像 题目:请完成一个函数,输入一个二叉树,该函数输出它的镜像.例如下图所示,左图是原二叉树,而右图则是该二叉树的镜像. 该二叉树节点的定义如下,采用C#语言描述: public c ...
- 剑指offer-第四章解决面试题的思路(二叉树的镜像)
题目:请完成函数,输入一个二叉树,该函数输出它的镜像. 思路:可能没有听说过书的镜像,但是可以通过画图等来找灵感.就像照镜子一样,人的左边和右边交换了. 如图: 通过如下图变化就可以由左图得到右图: ...
- 剑指offer 19:二叉树的镜像
题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述: 解题思路 这一问题明显,在进行递归遍历节点时,将根节点的左右子树进行交换,因此完成树的遍历即可. C++实现代码 /* ...
- 《剑指offer》第二十七题(二叉树的镜像)
// 面试题27:二叉树的镜像 // 题目:请完成一个函数,输入一个二叉树,该函数输出它的镜像. #include <iostream> #include "BinaryTree ...
随机推荐
- HTML5+J2EE实现文件异步上传
P.S. HTML5经过了W3C的8年努力,终于正式推广了.这次升级最大的就是升级了XMLHTTPRequest,让它变成了XMLHTTPRequest Level II(这有啥奇怪的?).这个对象现 ...
- 【Qt】关于Qt【转】
什么是Qt Qt是一个针对桌面.嵌入式.移动设备的一个跨平台的应用程序开发框架,支持的平台包括Linux.OS X.Windows.VxWorks.QNX.Android.iOS.BlackBerry ...
- 【原】web页面登陆验证
using Itcast.Mall.Model; using System; using System.Collections.Generic; using System.Linq; using Sy ...
- [总结]Android系统体系结构
Android 从图中可以看出Android主要的组成部分,其中底层是Linux的内核,包括的主要就是文件.内存.系统资源等的管理,Google在这部分的工作主要就是电源管理和一部分驱动,并且整合上层 ...
- hadoop 文件 复制 移动 FileUtil.copy
FileSystem fs = FileSystem.get(conf); Path pattern = new Path(resultViewPath + "phone*"); ...
- python学习之socket创建html服务器
#coding:utf-8 import socket def process_request(request): request.recv(1024)#读取接受的字节 request.send(&q ...
- 我的第一个canvas的作品:漫画对白编辑器
背景:一直都对canvas挺有有兴趣的,之前刚刚看了<HTML5 CANVAS基础教程>,写了篇读书笔记. 起因:老婆发来一张最近比较热的漫画图(友谊的小船说翻就翻什么的).这种漫画,经常 ...
- 考研路之C语言
今天在学习C的时候,又学到了一些新的内容,所以赶紧记录下来. case 1: #include <stdio.h> union exa{ struct{ int a; int b; }ou ...
- Android判断用户是平板还是手机的方法
public boolean isTabletDevice() { TelephonyManager telephony = (TelephonyManager) mContext.ge ...
- CentOS 6.5 安装与配置LAMP
准备工作: 1.配置防火墙,开启80端口.3306端口vi /etc/sysconfig/iptables-A INPUT -m state --state NEW -m tcp -p tcp --d ...