题目:请完成一个函数,输入一个二叉树,该函数输出它的镜像
二叉树结点的定义如下:
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. Chrome插件:网页截图

    截图(Webpage Screenshot)是一款Chrome浏览器中的截图插件,使用它可以快速地截取网页中的全部内容. 这是介绍地址:http://chromecj.com/blogging/201 ...

  2. php学习问题记录

    1.微信应用开发,Token认证失败,解决方法php页面加上一句代码: header('content-type:text');

  3. dtcms,header显示头像和用户名,QQ互联老不通过的解决方法

    http://bbs.dtsoft.net/forum.php?mod=viewthread&tid=1742&extra=page%3D1

  4. php微信支付(仅Jsapi支付)详细步骤.----仅适合第一次做微信开发的程序员

    本人最近做了微信支付开发,是第一次接触.其中走了很多弯路,遇到的问题也很多.为了让和我一样的新人不再遇到类似的问题,我把我的开发步骤和问题写出来,以供参考. 开发时间是2016/8/2,所以微信支付的 ...

  5. Websocket协议之php实现

    前面学习了HTML5中websocket的握手协议.打开和关闭连接等基础内容,最近用php实现了与浏览器websocket的双向通信.在学习概念的时候觉得看懂了的内容,真正在实践过程中还是会遇到各种问 ...

  6. HTTP的长短连接、长短轮询的区别(转载)

    引言 最近刚到公司不到一个月,正处于熟悉项目和源码的阶段,因此最近经常会看一些源码.在研究一个项目的时候,源码里面用到了HTTP的长轮询.由于之前没太接触过,因此LZ便趁着这个机会,好好了解了一下HT ...

  7. nginx禁止访问某个后缀名的文件

    猛然发现通过http://ip:端口号/路径/config.ini竟然能下载项目代码的配置文件,检查nginx配置,原来是没有加限制,立即加上,并记录如下: location ~* \.(ini|do ...

  8. Nat网络地址转换

    Nat中的术语 -------------------------------------------------------------------------------------------- ...

  9. Spark Streaming揭秘 Day15 No Receivers方式思考

    Spark Streaming揭秘 Day15 No Receivers方式思考 在前面也有比较多的篇幅介绍了Receiver在SparkStreaming中的应用,但是我们也会发现,传统的Recei ...

  10. realloc() 用法详解

    原型:extern void *realloc(void *mem_address, unsigned int newsize); 语法:指针名=(数据类型*)realloc(要改变内存大小的指针名, ...