《剑指offer》第七题(重要!重建二叉树)
文件一:main.cpp
// 面试题:重建二叉树
// 题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输
// 入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,
// 2, 4, 7, 3, 5, 6, 8}和中序遍历序列{4, 7, 2, 1, 5, 3, 8, 6},则重建出
// 图2.6所示的二叉树并输出它的头结点。 #include <iostream>
#include "BinaryTree.h"
using namespace std; BinaryTreeNode* ConstructCore(int* startPreorder, int* endPreorder, int* startInorder, int* endInorder); BinaryTreeNode* Construct(int* preorder, int* inorder, int length)
{
if (preorder == NULL || inorder == NULL || length <= )//确认输入存在
return NULL; return ConstructCore(preorder, preorder + length - ,inorder, inorder + length - );
} BinaryTreeNode* ConstructCore(int* startPreorder, int* endPreorder,int* startInorder, int* endInorder)//注意传入的是地址
{
// 前序遍历序列的第一个数字是根结点的值
BinaryTreeNode* root = CreateBinaryTreeNode(startPreorder[]);//建立根节点 if (startPreorder == endPreorder)//如果这个树只有根节点
{
if (startInorder == endInorder && *startPreorder == *startInorder)
return root;
else //注意判断输入是否真的是对的
throw exception("Invalid input.");
} // 在中序遍历中找到根结点的值
int* rootInorder = startInorder;
while (rootInorder <= endInorder && *rootInorder != startPreorder[])
++rootInorder; if (rootInorder == endInorder && *rootInorder != startPreorder[])//如果中序遍历中没有根节点,就抛出异常
throw exception("Invalid input."); int leftLength = rootInorder - startInorder;//计算左孩子子树个数
int* leftPreorderEnd = startPreorder + leftLength;
if (leftLength > )//递归的构建子树
{
// 构建左子树
root->m_pLeft = ConstructCore(startPreorder + , leftPreorderEnd,startInorder, rootInorder - );
}
if (leftLength < endPreorder - startPreorder)
{
// 构建右子树
root->m_pRight = ConstructCore(leftPreorderEnd + , endPreorder,rootInorder + , endInorder);
} return root;
} // ====================测试代码====================
void Test(const char* testName, int* preorder, int* inorder, int length)
{
if (testName != NULL)
cout << testName << " begins:\n"; cout << "The preorder sequence is: ";
for (int i = ; i < length; ++i)
cout << preorder[i];
cout << endl; cout << "The inorder sequence is: ";
for (int i = ; i < length; ++i)
cout << inorder[i];
cout << endl; try
{
BinaryTreeNode* root = Construct(preorder, inorder, length);
PrintTree(root); DestroyTree(root);
}
catch (exception& exception)
{
cout << "Invalid Input.\n";
}
} // 普通二叉树
// 1
// / \
// 2 3
// / / \
// 4 5 6
// \ /
// 7 8
void Test1()
{
const int length = ;
int preorder[length] = { , , , , , , , };
int inorder[length] = { , , , , , , , }; Test("Test1", preorder, inorder, length);
} // 所有结点都没有右子结点
// 1
// /
// 2
// /
// 3
// /
// 4
// /
//
void Test2()
{
const int length = ;
int preorder[length] = { , , , , };
int inorder[length] = { , , , , }; Test("Test2", preorder, inorder, length);
} // 所有结点都没有左子结点
// 1
// \
// 2
// \
// 3
// \
// 4
// \
// 5
void Test3()
{
const int length = ;
int preorder[length] = { , , , , };
int inorder[length] = { , , , , }; Test("Test3", preorder, inorder, length);
} // 树中只有一个结点
void Test4()
{
const int length = ;
int preorder[length] = { };
int inorder[length] = { }; Test("Test4", preorder, inorder, length);
} // 完全二叉树
// 1
// / \
// 2 3
// / \ / \
// 4 5 6 7
void Test5()
{
const int length = ;
int preorder[length] = { , , , , , , };
int inorder[length] = { , , , , , , }; Test("Test5", preorder, inorder, length);
} // 输入空指针
void Test6()
{
Test("Test6", NULL, NULL, );
} // 输入的两个序列不匹配
void Test7()
{
const int length = ;
int preorder[length] = { , , , , , , };
int inorder[length] = { , , , , , , }; Test("Test7: for unmatched input", preorder, inorder, length);
} int main(int argc, char* argv[])
{
Test1();
Test2();
Test3();
Test4();
Test5();
Test6();
Test7(); system("pause");
}
文件二:BinaryTree.h
#ifndef BINARY_TREE_H
#define BINARY_TREE_H struct BinaryTreeNode
{
int m_nValue;
BinaryTreeNode* m_pLeft;
BinaryTreeNode* m_pRight;
}; BinaryTreeNode* CreateBinaryTreeNode(int value);
void ConnectTreeNodes(BinaryTreeNode* pParent, BinaryTreeNode* pLeft, BinaryTreeNode* pRight);
void PrintTreeNode(const BinaryTreeNode* pNode);
void PrintTree(const BinaryTreeNode* pRoot);
void DestroyTree(BinaryTreeNode* pRoot); #endif
文件三:BinaryTree.cpp
#include <iostream>
#include "BinaryTree.h"
using namespace std; BinaryTreeNode* CreateBinaryTreeNode(int value)//创建一个二叉树节点
{
BinaryTreeNode* pNode = new BinaryTreeNode();
pNode->m_nValue = value;
pNode->m_pLeft = NULL;
pNode->m_pRight = NULL; return pNode;
} void ConnectTreeNodes(BinaryTreeNode* pParent, BinaryTreeNode* pLeft, BinaryTreeNode* pRight)//将两个孩子连接到一个父节点
{
if (pParent != NULL)
{
pParent->m_pLeft = pLeft;
pParent->m_pRight = pRight;
}
} void PrintTreeNode(const BinaryTreeNode* pNode)//打印当前二叉树节点
{
if (pNode != NULL)//判断该节点存在否
{
cout << "value of this node is:" << pNode->m_nValue << endl;//打印父节点 if (pNode->m_pLeft != NULL)//打印左孩子节点
cout << "value of its left child is:" << pNode->m_pLeft->m_nValue << endl;
else
cout << "left child is NULL.\n"; if (pNode->m_pRight != NULL)//打印右孩子节点
cout << "value of its right child is:" << pNode->m_pRight->m_nValue << endl;
else
cout << "right child is NULL.\n";
}
else
{
cout << "this node is nullptr.\n";
} cout << endl;
} void PrintTree(const 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);
}
}
《剑指offer》第七题(重要!重建二叉树)的更多相关文章
- C++版-剑指offer 面试题6:重建二叉树(Leetcode105. Construct Binary Tree from Preorder and Inorder Traversal) 解题报告
剑指offer 重建二叉树 提交网址: http://www.nowcoder.com/practice/8a19cbe657394eeaac2f6ea9b0f6fcf6?tpId=13&tq ...
- 剑指Offer(四):重建二叉树
一.前言 刷题平台:牛客网 二.题目 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6, ...
- 剑指offer 面试题7:重建二叉树
题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7, ...
- 剑指Offer面试题:5.重建二叉树
一.题目:重建二叉树 题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序 ...
- 剑指offer 面试题6:重建二叉树
重建二叉树 题目 输入某二叉树的前序遍历和中序遍历,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含有重复的数字. 例如,前序遍历序列:{1,2,3,7,3,5,6,8},中序遍历序列:{ ...
- 剑指offer——面试题7:重建二叉树
// 面试题7:重建二叉树 // 题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输 // 入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1, // 2, ...
- 剑指Offer(书):重建二叉树
题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2, ...
- 《剑指offer》面试题6 重建二叉树 Java版
(由一个二叉树的前序和中序序列重建一颗二叉树) 书中方法:我们要重建一棵二叉树,就要不断地找到根节点和根节点的左子结点和右子节点.注意前序序列, 它的第一个元素就是二叉树的根节点,后面的元素分为它的左 ...
- [刷题] 剑指Offer 面试题7:重建二叉树
题目:输入某二叉树的前序遍历和中序遍历结果,重建该二叉树.(假设输入的前序和中序遍历结果中都不含重复数字) 思路 构建二叉树的两个函数:Construct().ConstructCore() Cons ...
- 剑指offer面试题6:重建二叉树
1.题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树. public class Solution { public TreeNode reConstructBinaryTree(int ...
随机推荐
- SpringMVC学习笔记二第一个小的程序
首先:我们要用springmvc来写一个helloworld的例子: 首先我们需要导入所需要的架包: /demo1/WebRoot/WEB-INF/lib/commons-logging-1.1.1. ...
- vs计算代码行数
1.用vs打开程序 2.编辑——查找——在文件中查找 3.查找内容^b*[^:b#/]+.*$ 应用正则表达式,在整个解决方案中,文件类型空 4.查找全部,仔细盯着右下角数字,查找完毕后会自动消失 ...
- Eclipse 启动项目错误:class not found
其中,很可能的原因:项目存在编译错误,根本没有编译成功,没有生成class文件:可查看problems标签页查看具体错误.
- 最新版Intellij IDEA插件JRebel 7.0.7官方免费激活
本文转自:http://blog.csdn.net/u012283609/article/details/70213307 开场语 有时候真实比小说更加荒诞,因为虚构是在一定逻辑下进行的,而现实往往毫 ...
- (一)MySQL登录与退出
mysql登陆: win+r输入cmd按enter进入命令行界面: > mysql -uroot -p -P3306 -h127.0.0.1 > 输入密码后按回车 mysql退出: mys ...
- CentOS 7 安装OpenCV
CentOS 7 安装OpenCV步骤如下: 1.在CentOS 7命令行中直接在线安装: yum install numpy opencv* 2.安装完成后进行全盘搜索:find / -n ...
- 一个远程启动windows c++程序引发的技术决策现象
还是因为那个8点半前要启动近百套报盘程序的问题,差不多两周前表示自己会抽空给解决掉,一次性启动,直到昨天才差不多能够抽点时间出来开始想怎么解决的问题. 这个问题的复杂点在于除了启动exe外,还需要鼠标 ...
- HTML 和 JavaScript 编写简单的 404 界面
编写简单的 404 界面,也可以用来做 500 报错界面,还会飘东西,特别好,蛮漂亮的! <!DOCTYPE html> <html> <head> <met ...
- SIFT特征原理简析(HELU版)
SIFT(Scale-Invariant Feature Transform)是一种具有尺度不变性和光照不变性的特征描述子,也同时是一套特征提取的理论,首次由D. G. Lowe于2004年以< ...
- Disruptor学习笔记(一):基本原理和概念
一.Disruptor基本原理 在多线程开发中,我们常常遇到这样一种场景:一些线程接受用户请求,另外一些线程处理这些请求.比如日志处理中的日志输入和告警.这种典型的生产者消费者场景十分常见,而生产者消 ...