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 ...
随机推荐
- Java中的数组排序
Java中的数组排序,一般是利用Arrays.sort(),这个方法是经过优化的快速排序.在Arrays种有多中形式的重载,在这里就不一一列举了. 数组排序的种类: 1.非降序排序, 非升序排序(就排 ...
- 在HTML5中怎样实现Canvas阴影效果
该文章是由e良师益友技术部小陈原创作品,转载是请注明来源,谢谢! 今天我给大家介绍一下在HTML5中怎样实现Canvas阴影效果,我们知道现在HTML5的Canvas阴影也经常使用的,这个就是HTML ...
- js定义参数默认值
javascript可以用arguments定义参数组. 一.简单的定义参数默认值 function test1(a,b){ //如果有参数一,则返回参数一,如果没有返回默认值"这是参数 ...
- TreeView递归取值
string jingyuan = ""; string jinghui = ""; private void DiGui(TreeNode tn) { if ...
- C#使用Zxing2.0生成二维码 带简单中心LOGO
参考:http://www.open-open.com/lib/view/open1379214678162.html 代码:http://files.cnblogs.com/halo/%E4%BA% ...
- html5 的<audio> 音频 audio的“坑”
<audio>标签是html5的一个非常有意义的特性.告别的flash的时代.它的属性有: autoplay:音频就绪后马上播放 controls:出现该属性,向用户显示播放的控件. lo ...
- php 操作数组 (合并,拆分,追加,查找,删除等)
1. 合并数组 array_merge()函数将数组合并到一起,返回一个联合的数组.所得到的数组以第一个输入数组参数开始,按后面数组参数出现的顺序依次迫加.其形式为: array array_merg ...
- java之StringBuffer
StringBuffer就是字符串缓冲区,用于存储数据的容器. 特点:长度可变,可存储不同类型的数据,最终转化成字符串使用,可以对字符串修改 功能: 添加:append(value), insert( ...
- max_size, capacity and size 的区别
The max_size() function returns the maximum number of elements that the container can hold. The max_ ...
- android.support.v7.app.AppCompatActivity
1.Android Studio (api 23) 新建项目的时候 Activity public class MainActivity extends AppCompatActivity 2.系统默 ...