博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
《剑指offer》第七题(重要!重建二叉树)
阅读量:4364 次
发布时间:2019-06-07

本文共 6580 字,大约阅读时间需要 21 分钟。

文件一:main.cpp

// 面试题:重建二叉树// 题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输// 入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,// 2, 4, 7, 3, 5, 6, 8}和中序遍历序列{4, 7, 2, 1, 5, 3, 8, 6},则重建出// 图2.6所示的二叉树并输出它的头结点。#include 
#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 <= 0)//确认输入存在 return NULL; return ConstructCore(preorder, preorder + length - 1,inorder, inorder + length - 1);}BinaryTreeNode* ConstructCore(int* startPreorder, int* endPreorder,int* startInorder, int* endInorder)//注意传入的是地址{ // 前序遍历序列的第一个数字是根结点的值 BinaryTreeNode* root = CreateBinaryTreeNode(startPreorder[0]);//建立根节点 if (startPreorder == endPreorder)//如果这个树只有根节点 { if (startInorder == endInorder && *startPreorder == *startInorder) return root; else //注意判断输入是否真的是对的 throw exception("Invalid input."); } // 在中序遍历中找到根结点的值 int* rootInorder = startInorder; while (rootInorder <= endInorder && *rootInorder != startPreorder[0]) ++rootInorder; if (rootInorder == endInorder && *rootInorder != startPreorder[0])//如果中序遍历中没有根节点,就抛出异常 throw exception("Invalid input."); int leftLength = rootInorder - startInorder;//计算左孩子子树个数 int* leftPreorderEnd = startPreorder + leftLength; if (leftLength > 0)//递归的构建子树 { // 构建左子树 root->m_pLeft = ConstructCore(startPreorder + 1, leftPreorderEnd,startInorder, rootInorder - 1); } if (leftLength < endPreorder - startPreorder) { // 构建右子树 root->m_pRight = ConstructCore(leftPreorderEnd + 1, endPreorder,rootInorder + 1, 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 = 0; i < length; ++i) cout << preorder[i]; cout << endl; cout << "The inorder sequence is: "; for (int i = 0; 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 8void Test1(){ const int length = 8; int preorder[length] = { 1, 2, 4, 7, 3, 5, 6, 8 }; int inorder[length] = { 4, 7, 2, 1, 5, 3, 8, 6 }; Test("Test1", preorder, inorder, length);}// 所有结点都没有右子结点// 1// / // 2 // / // 3 // /// 4// /// 5void Test2(){ const int length = 5; int preorder[length] = { 1, 2, 3, 4, 5 }; int inorder[length] = { 5, 4, 3, 2, 1 }; Test("Test2", preorder, inorder, length);}// 所有结点都没有左子结点// 1// \ // 2 // \ // 3 // \// 4// \// 5void Test3(){ const int length = 5; int preorder[length] = { 1, 2, 3, 4, 5 }; int inorder[length] = { 1, 2, 3, 4, 5 }; Test("Test3", preorder, inorder, length);}// 树中只有一个结点void Test4(){ const int length = 1; int preorder[length] = { 1 }; int inorder[length] = { 1 }; Test("Test4", preorder, inorder, length);}// 完全二叉树// 1// / \// 2 3 // / \ / \// 4 5 6 7void Test5(){ const int length = 7; int preorder[length] = { 1, 2, 4, 5, 3, 6, 7 }; int inorder[length] = { 4, 2, 5, 1, 6, 3, 7 }; Test("Test5", preorder, inorder, length);}// 输入空指针void Test6(){ Test("Test6", NULL, NULL, 0);}// 输入的两个序列不匹配void Test7(){ const int length = 7; int preorder[length] = { 1, 2, 4, 5, 3, 6, 7 }; int inorder[length] = { 4, 2, 8, 1, 6, 3, 7 }; 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_Hstruct 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 
#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); }}

 

转载于:https://www.cnblogs.com/CJT-blog/p/10467058.html

你可能感兴趣的文章
xftp上传文件失败,执行程序发现磁盘满了:No space left on device
查看>>
duplicate symbols for architecture i386 问题?
查看>>
[BZOJ]1027 合金(JSOI2007)
查看>>
poj 1696 Space Ant (几何 : 叉积 应用 + dfs)
查看>>
MySQL:按前缀批量删除表格
查看>>
Route学习笔记之Area的Route注册
查看>>
构建之法--软件工程师自我测评表
查看>>
电子书搜索
查看>>
SQO2008配置管理工具服务显示远程过程调用失败
查看>>
【HDOJ】1009 FatMouse' Trade
查看>>
谷歌跨域
查看>>
使用葡萄城报表,轻松实现高度精准的报表套打
查看>>
Linux命令
查看>>
unicode ascii 互转 函数 C实现 MultiByteToWideChar/WideCharToMultiByte 详解
查看>>
大三第一学期实验报告
查看>>
mysql远程链接
查看>>
nginx location配置
查看>>
Easy Install详细参数
查看>>
选课系统
查看>>
最简实例演示asp.net5中用户认证和授权(2)
查看>>