剑指offer-第四章解决面试题的思路(二叉树的镜像)
题目:请完成函数,输入一个二叉树,该函数输出它的镜像。
思路:可能没有听说过书的镜像,但是可以通过画图等来找灵感。就像照镜子一样,人的左边和右边交换了。
如图:

通过如下图变化就可以由左图得到右图:

总体来说:将所有的非叶子节点的左右子树交换。由于交换的过程惊人的一直,因此可以采用递归。
C++代码:
#include<iostream>
using namespace std;
struct BinaryTreeNode
{
int m_nValue;
BinaryTreeNode* m_pLeft;
BinaryTreeNode* m_pRight;
};
BinaryTreeNode* ConstructCore(int* startPreorder,int* endPreorder,int* startInorder,int* endInorder)
{
int rootValue=startPreorder[0];
BinaryTreeNode* root=new BinaryTreeNode();
root->m_nValue=rootValue;
root->m_pLeft=root->m_pRight=NULL;
if(startPreorder==endPreorder)
{
if(startInorder==endInorder&&*startPreorder==*startInorder)
{
return root;
}
else
throw std::exception("Invalid put!");
}
//通过中序遍历序列找到根节点
int* rootInorder=startInorder;
while(rootInorder<=endInorder&&*rootInorder!=rootValue)
{
++rootInorder;
}
if(rootInorder==endInorder&&*rootInorder!=rootValue)
{
throw std::exception("Invalid put");
}
int leftLength=rootInorder-startInorder;
int rightLength=endInorder-rootInorder;
int* leftPreorderEnd=startPreorder+leftLength;
if(leftLength>0)
{
//递归构建左子树
root->m_pLeft=ConstructCore(startPreorder+1,leftPreorderEnd,startInorder,rootInorder-1);
}
if(rightLength>0)
{
//递归构建右子树
root->m_pRight=ConstructCore(leftPreorderEnd+1,endPreorder,rootInorder+1,endInorder);
}
return root;
} BinaryTreeNode* Construct(int* preorder,int* inorder,int length)
{
if(preorder==NULL||inorder==NULL||length<=0)
{
throw std::exception("Invalid put!");
}
return ConstructCore(preorder,preorder+length-1,inorder,inorder+length-1);
}
void swrap_element(BinaryTreeNode** left,BinaryTreeNode** right)
{
BinaryTreeNode* temp=*left;
*left=*right;
*right=temp;
}
void MirrorRecursively(BinaryTreeNode* pNode)
{
if(pNode==NULL||(pNode->m_pLeft==NULL&&pNode->m_pRight))
return;
swrap_element(&pNode->m_pLeft,&pNode->m_pRight);
if(pNode->m_pLeft!=NULL)
MirrorRecursively(pNode->m_pLeft);
if(pNode->m_pRight!=NULL)
MirrorRecursively(pNode->m_pRight);
}
void PrintTreeNode(BinaryTreeNode* pNode) {
if(pNode != NULL)
{
printf("value of this node is: %d\n", pNode->m_nValue);
if(pNode->m_pLeft != NULL)
printf("value of its left child is: %d.\n", pNode->m_pLeft->m_nValue);
else
printf("left child is null.\n");
if(pNode->m_pRight != NULL)
printf("value of its right child is: %d.\n", pNode->m_pRight->m_nValue);
else
printf("right child is null.\n");
}
else
{
printf("this node is null.\n");
}
printf("\n");
} //递归打印左右子树
void PrintTree(BinaryTreeNode* pRoot)
{
PrintTreeNode(pRoot);
if(pRoot != NULL)
{
if(pRoot->m_pLeft != NULL)
PrintTree(pRoot->m_pLeft);
if(pRoot->m_pRight != NULL)
PrintTree(pRoot->m_pRight);
}
}
//递归删除左右子树 void DestroyTree(BinaryTreeNode* pRoot)
{
if(pRoot != NULL)
{
BinaryTreeNode* pLeft = pRoot->m_pLeft;
BinaryTreeNode* pRight = pRoot->m_pRight;
delete pRoot;
pRoot = NULL;
DestroyTree(pLeft);
DestroyTree(pRight);
}
} void main()
{
const int length1 = 8; int preorder1[length1] = {1, 2, 4, 7, 3, 5, 6, 8};
int inorder1[length1] = {4, 7, 2, 1, 5, 3, 8, 6}; BinaryTreeNode *root1 = Construct(preorder1, inorder1, length1); PrintTree(root1);
MirrorRecursively(root1);
PrintTree(root1); }
Java代码:
public class MirrorRecursively {
public static class BinaryTreeNode{
int m_nValue;
BinaryTreeNode m_pLeft;
BinaryTreeNode m_pRight;
}
public static BinaryTreeNode ConstructBiTree(int[] preOrder,int start,int[] inOrder,int end,int length){
if(preOrder==null||inOrder==null||preOrder.length!=inOrder.length||length<=0)
return null;
int value=preOrder[start];
BinaryTreeNode root=new BinaryTreeNode();
root.m_nValue=value;
root.m_pLeft=root.m_pRight=null;
//当整棵树只有一个节点的情况
if(length==1){
if(value==inOrder[end])
return root;
else
throw new RuntimeException("inVaild put");
}
//在中序遍历的数组中找到根节点
int i=0;
while(i<length){
if(value==inOrder[end-i]){
break;
}
i++;
}
if(i==length)
throw new RuntimeException("inVaild put!");
root.m_pLeft=ConstructBiTree(preOrder,start+1,inOrder,end-i-1,length-i-1);
root.m_pRight=ConstructBiTree(preOrder,start+length-i,inOrder,end,i);
return root;
}
public static void printNode(BinaryTreeNode pNode){
if(pNode==null)
return;
System.out.println("the node is:"+pNode.m_nValue);
if(pNode.m_pLeft!=null)
System.out.println("the left node is:"+pNode.m_pLeft.m_nValue);
else
System.out.println("the left node is null");
if(pNode.m_pRight!=null)
System.out.println("the right node is:"+pNode.m_pRight.m_nValue);
else
System.out.println("the right node is null");
}
public static void printBiTree(BinaryTreeNode pNode){
printNode(pNode);
if(pNode.m_pLeft!=null)
printBiTree(pNode.m_pLeft);
if(pNode.m_pRight!=null)
printBiTree(pNode.m_pRight);
}
//二叉树的镜像
public static void MirrorRecursive(BinaryTreeNode pHead){
if(pHead==null||(pHead.m_pLeft==null&&pHead.m_pRight==null))
return;
BinaryTreeNode pTemp=pHead.m_pLeft;
pHead.m_pLeft=pHead.m_pRight;
pHead.m_pRight=pTemp;
if(pHead.m_pLeft!=null)
MirrorRecursive(pHead.m_pLeft);
if(pHead.m_pRight!=null)
MirrorRecursive(pHead.m_pRight);
}
public static void main(String[] args){
int[] preOrder={3,6};
int[] inOrder={6,3};
BinaryTreeNode pNode=ConstructBiTree(preOrder,0,inOrder,1,preOrder.length);
printBiTree(pNode);
MirrorRecursive(pNode);
printBiTree(pNode);
}
}
剑指offer-第四章解决面试题的思路(二叉树的镜像)的更多相关文章
- 剑指offer第四章
剑指offer第四章 1.二叉树的镜像 二叉树的镜像:输入一个二叉树,输出它的镜像 分析:求树的镜像过程其实就是在遍历树的同时,交换非叶结点的左右子结点. 求镜像的过程:先前序遍历这棵树的每个结点,如 ...
- 剑指offer编程题Java实现——面试题6重建二叉树
题目: 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2, ...
- 剑指offer-第四章解决面试题的思路(从上往下打印二叉树)
题目:从上往下打印二叉树的每一个节点,同一层的节点按照从左到右的顺序打印 思路:这是一个层序遍历的问题,因此要借用到队列.我们可以在打印第一个节点的同时将这个节点的左右子节点都放入队列,同样打印左右子 ...
- 剑指offer-第四章解决面试题的思路(栈的压入和弹出序列)
题目:输入两个整数序列,第一个序列表示栈的压入序列,请判断第二个序列是否为弹出序列. 思路:定义两个指针sPush和sPop分别指向两个整数序列的开头,借助一个辅助的栈,将第一个序列的数据依次压入栈中 ...
- 剑指offer-第四章解决面试题的思路(包含min函数的栈)
题目:定义栈的数据结构,请在该类型中实现一个能够得到栈的最小元素的min函数,在该栈中,调用min,push及pop的时间复杂度都是O(1) 思路:定义两个栈分别为dataStack和minStack ...
- 剑指offer-第四章解决面试题的思路(顺序打印矩阵)
题目:输入一个矩阵,按照从外向里的顺序依次打印出每一个数.(画图让抽象的问题形象化) 思路:打印矩阵时,把每一层当做一个圈来打印,找到打印整个矩阵的截止条件. 从上图中我可以看到一个6*6的矩阵(长宽 ...
- 剑指offer第五章
剑指offer第五章 1.数组中出现次数超过一半的数 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字. 例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组 ...
- 剑指offer第七章&第八章
剑指offer第七章&第八章 1.把字符串转换成整数 将一个字符串转换成一个整数,要求不能使用字符串转换整数的库函数. 数值为0或者字符串不是一个合法的数值则返回0 输入描述: 输入一个字符串 ...
- 剑指offer第六章
剑指offer第六章 1.数字在排序数组中出现的次数 统计一个数字在排序数组中出现的次数.例如输入排序数组{1,2,3,3,3,3,4,5}和数字3,由于3在数组中出现了4次,所以输出4 分析:思路1 ...
随机推荐
- 解决 SMTP Error: Could not connect to SMTP host. 问题
我在使用PHPmailer发邮件时候,遇到了这个问题“SMTP Error: Could not connect to SMTP host.”,分享一下解决方法. 这个错误是PHP版本7产生的.如果我 ...
- Linux内核中的信号机制--一个简单的例子【转】
本文转载自:http://blog.csdn.net/ce123_zhouwei/article/details/8562958 Linux内核中的信号机制--一个简单的例子 Author:ce123 ...
- mongodb 的Cursor 作为 stream 的时候,读出来的数据数字开头的key没法访问(又踩了一个坑)
mongdb 用Cursor 读取数据的时候,直接用流读出来的数据key是数字开头的话,就是独具不到,用Object.keys() 把所有的key 打印出来的话如下:怎么会是这样的呢? 查看了一下文档 ...
- MySQL的索引实现原理
MySQL数据库索引总结使用索引的原由数据结构Hash.平衡二叉树.B树.B+树区别机械硬盘.固态硬盘区别Myisam与Innodb B+树的区别MySQL中的索引什么数据结构B+树中的节点到底存放多 ...
- 织梦导航 currentstyle 点击li添加class类 样式
<!--导航开始--> <div class="global_nav_wrap"> <ul class="nav nav-pills&quo ...
- java基础(6)--数组和方法
数组 1. 什么是数组? 数组是相同数据类型的元素组成的集合.这些元素按线性顺序排列.所谓线性顺序是指除第一个元素外,每一个元素都有唯一的前驱元素:除最后一个元素外,每一个元素都有唯一的后继元素.(“ ...
- xml的servlet配置
内容如下 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="ht ...
- angularjs实现星星评分
angularjs实现星星评分 自定义指令 app.directive('myStars', function () { return { require : '?ngModel', // ?ngMo ...
- js 小复习2
1.数组 findIndex() indexOf() // findIndex()方法返回数组中满足提供的测试函数的第一个元素的索引.否则返回-1. function isBigEnough(ele ...
- OWIN初探
什么是 OWIN ? OWIN 的全称是 "Open Web Interface for .NET", OWIN 在 .NET Web 服务器和 .NET Web 应用之间定义了一 ...