题目:请完成一个函数,输入一个二叉树,该函数输出它的镜像
二叉树结点的定义如下:
struct BinaryTreeNode{
       int     m_nValue;
       BinaryTreeNode*     m_pLeft;
       BinaryTreeNode*     m_pRight;
}

求一棵树的镜像的过程:我们先前序比遍历这棵树的每个结点,如果遍历到的结点有子结点,就交换它的两个子结点。当交换完所有非叶子结点的左右子结点之后,就得到了树的镜像。
 
测试用例:
1)功能测试(普通二叉树,二叉树的所有结点都没有左子树或者右子树,只有一个结点的二叉树);
2)特殊输入测试(二叉树的根节点为null指针)。
代码实现:
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();
}
}
 
输出结果:
=====Test1 starts:=====
previous printing tree is:
8 6 5 7 10 9 11
middle printing tree is:
5 6 7 8 9 10 11
=====Test1: MirrorRecursively=====
previous printing tree is:
8 10 11 9 6 7 5
middle printing tree is:
11 10 9 8 7 6 5
=====Test1: MirrorIteratively=====
previous printing tree is:
8 6 5 7 10 9 11
middle printing tree is:
5 6 7 8 9 10 11
 
=====Test2 starts:=====
previous printing tree is:
8 7 6 5 4
middle printing tree is:
4 5 6 7 8
=====Test2: MirrorRecursively=====
previous printing tree is:
8 7 6 5 4
middle printing tree is:
8 7 6 5 4
=====Test2: MirrorIteratively=====
previous printing tree is:
8 7 6 5 4
middle printing tree is:
4 5 6 7 8
 
=====Test3 starts:=====
previous printing tree is:
8 7 6 5 4
middle printing tree is:
8 7 6 5 4
=====Test3: MirrorRecursively=====
previous printing tree is:
8 7 6 5 4
middle printing tree is:
4 5 6 7 8
=====Test3: MirrorIteratively=====
previous printing tree is:
8 7 6 5 4
middle printing tree is:
8 7 6 5 4
 
=====Test4 starts:=====
previous printing tree is:
 
middle printing tree is:
 
=====Test4: MirrorRecursively=====
previous printing tree is:
 
middle printing tree is:
 
=====Test4: MirrorIteratively=====
previous printing tree is:
 
middle printing tree is:
 
 
=====Test5 starts:=====
previous printing tree is:
8
middle printing tree is:
8
=====Test5: MirrorRecursively=====
previous printing tree is:
 
middle printing tree is:
 
=====Test5: MirrorIteratively=====
previous printing tree is:
 
middle printing tree is:

P125、面试题19:二叉树的镜像的更多相关文章

  1. 剑指Offer:面试题19——二叉树的镜像(java实现)

    问题描述: 操作给定的二叉树,将其变换为源二叉树的镜像. 二叉树结点定义为: public class TreeNode { int val = 0; TreeNode left = null; Tr ...

  2. 剑指offer面试题19 二叉树的镜像

    题目描述 操作给定的二叉树,将其变换为源二叉树的镜像.  输入描述 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ ...

  3. 《剑指offer》面试题19 二叉树的镜像 Java版

    书中方法:这道题目可能拿到手没有思路,我们可以在纸上画出简单的二叉树来找到规律.最后我们发现,镜像的实质是对于二叉树的所有节点,交换其左右子节点.搞清楚获得镜像的方法,这道题实际上就变成了一道二叉树遍 ...

  4. 【剑指Offer】面试题27. 二叉树的镜像

    题目 请完成一个函数,输入一个二叉树,该函数输出它的镜像. 例如输入:      4    /   \   2     7  / \   / \ 1   3 6   9 镜像输出:      4   ...

  5. 《剑指offer》面试题27. 二叉树的镜像

    问题描述 请完成一个函数,输入一个二叉树,该函数输出它的镜像. 例如输入:      4    /   \   2     7  / \   / \ 1   3 6   9 镜像输出:      4 ...

  6. (剑指Offer)面试题19:二叉树的镜像

    题目: 操作给定的二叉树,将其变换为源二叉树的镜像. 二叉树的定义如下: struct TreeNode{ int val; TreeNode* left; TreeNode* right; }; 输 ...

  7. 剑指Offer面试题:18.二叉树的镜像

    一.题目:二叉树的镜像 题目:请完成一个函数,输入一个二叉树,该函数输出它的镜像.例如下图所示,左图是原二叉树,而右图则是该二叉树的镜像. 该二叉树节点的定义如下,采用C#语言描述: public c ...

  8. 剑指offer-第四章解决面试题的思路(二叉树的镜像)

    题目:请完成函数,输入一个二叉树,该函数输出它的镜像. 思路:可能没有听说过书的镜像,但是可以通过画图等来找灵感.就像照镜子一样,人的左边和右边交换了. 如图: 通过如下图变化就可以由左图得到右图: ...

  9. 剑指offer 19:二叉树的镜像

    题目描述 操作给定的二叉树,将其变换为源二叉树的镜像.   输入描述:   解题思路 这一问题明显,在进行递归遍历节点时,将根节点的左右子树进行交换,因此完成树的遍历即可.   C++实现代码 /* ...

  10. 《剑指offer》第二十七题(二叉树的镜像)

    // 面试题27:二叉树的镜像 // 题目:请完成一个函数,输入一个二叉树,该函数输出它的镜像. #include <iostream> #include "BinaryTree ...

随机推荐

  1. 手动从浏览器中获取 cookie

    以 chrome 浏览器为例 1) 浏览器登陆人人网并打开一个人人网页面 2)打开 开发者工具,快捷键 Ctrl + Shift + I(大写 i) 3)切换到 console 页,输入 docume ...

  2. 编译安装php Cannot find MySQL header files under /usr/include/mysql.

    编译php-5.5-6的mysql支持,出现Cannot find MySQL header files under /usr/include/mysql. Note that the MySQL c ...

  3. 串操作,C++实现

    对串的基本操作都全已经实现 对kmp,kf字符串替换等功能全都已经实现 由于时间原因.没来得及注释,希望大家参考见谅. 串操作hstring.h头文件实现 //kallen 1 #ifndef _HS ...

  4. winForm 中子窗体关闭但不释放对象,方便下次继续打开

    方法一: 修改子窗体中自动生成的Dispose方法(在Form.Designer.cs文件中) /// <summary> /// Clean up any resources being ...

  5. c#中const与readonly区别

    const 的概念就是一个包含不能修改的值的变量.常数表达式是在编译时可被完全计算的表达式.因此不能从一个变量中提取的值来初始化常量.如果 const int a = b+1;b是一个变量,显然不能再 ...

  6. android eclipse集成环境

    Android开发工具(ADT)是一个插件为Eclipse IDE,它的目的是给你一个强大的,集成的环境来构建Android应用程序. ADT扩展了Eclipse的功能使用Android SDK工具, ...

  7. JS中如何判断null

    var exp = null; if (exp == null) { alert("is null"); } exp 为 undefined 时,也会得到与 null 相同的结果, ...

  8. linux下rm误删除数据库文件的恢复方法

    在linux redhat 5.4版本,rm误删除数据库文件的恢复过程分享.测试没有问题,可用. 1.首先测试rm 误删除数据库文件 [oracle@primary dbwdn]$ ll total ...

  9. 考研路之C语言

    今天在学习C的时候,又学到了一些新的内容,所以赶紧记录下来. case 1: #include <stdio.h> union exa{ struct{ int a; int b; }ou ...

  10. 【Vmware】已有镜像文件的导入

    1  虚拟机文件夹中各个文件简介 在创建虚拟机的时候会把相关的文件保存到一个文件夹中.我的机器是Windows 7,64位 ,保存的路径是: C:\Users\User\Documents\Virtu ...