文件一: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》第七题(重要!重建二叉树)的更多相关文章

  1. C++版-剑指offer 面试题6:重建二叉树(Leetcode105. Construct Binary Tree from Preorder and Inorder Traversal) 解题报告

    剑指offer 重建二叉树 提交网址: http://www.nowcoder.com/practice/8a19cbe657394eeaac2f6ea9b0f6fcf6?tpId=13&tq ...

  2. 剑指Offer(四):重建二叉树

    一.前言 刷题平台:牛客网 二.题目 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6, ...

  3. 剑指offer 面试题7:重建二叉树

    题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7, ...

  4. 剑指Offer面试题:5.重建二叉树

    一.题目:重建二叉树 题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序 ...

  5. 剑指offer 面试题6:重建二叉树

    重建二叉树 题目 输入某二叉树的前序遍历和中序遍历,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含有重复的数字. 例如,前序遍历序列:{1,2,3,7,3,5,6,8},中序遍历序列:{ ...

  6. 剑指offer——面试题7:重建二叉树

    // 面试题7:重建二叉树 // 题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输 // 入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1, // 2, ...

  7. 剑指Offer(书):重建二叉树

    题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2, ...

  8. 《剑指offer》面试题6 重建二叉树 Java版

    (由一个二叉树的前序和中序序列重建一颗二叉树) 书中方法:我们要重建一棵二叉树,就要不断地找到根节点和根节点的左子结点和右子节点.注意前序序列, 它的第一个元素就是二叉树的根节点,后面的元素分为它的左 ...

  9. [刷题] 剑指Offer 面试题7:重建二叉树

    题目:输入某二叉树的前序遍历和中序遍历结果,重建该二叉树.(假设输入的前序和中序遍历结果中都不含重复数字) 思路 构建二叉树的两个函数:Construct().ConstructCore() Cons ...

  10. 剑指offer面试题6:重建二叉树

    1.题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树. public class Solution { public TreeNode reConstructBinaryTree(int ...

随机推荐

  1. iOS UI基础-4.0应用程序管理

    功能与界面 功能分析: 以九宫格的形式展示应用信息 点击下载按钮后,做出相应的操作 步骤分析: 加载应用信息 根据应用的个数创建对应的view 监听下载按钮点击 整个应用界面: 程序实现 思路 UI布 ...

  2. js之数组排序

    数组,大家都不陌生,只要是学编程的人都知道这个入门的数据结构,在js中也是有数组这个概念的,跟普通的数组一样只是定义的形式不同罢了.下面是一个数组的排序代码: <html> <hea ...

  3. 全文搜索引擎Xapian

    安装过程 安装xapian-core wget http://oligarchy.co.uk/xapian/1.0.13/xapian-core-1.0.13.tar.gztar zxvf xapia ...

  4. WPF学习笔记-用Expression Design制作矢量图然后导出为XAML

    WPF学习笔记-用Expression Design制作矢量图然后导出为XAML 第一次用Windows live writer写东西,感觉不错,哈哈~~ 1.在白纸上完全凭感觉,想象来画图难度很大, ...

  5. zw版【转发·台湾nvp系列Delphi例程】HALCON Roberts1

    zw版[转发·台湾nvp系列Delphi例程]HALCON Roberts1 procedure TForm1.Button1Click(Sender: TObject);var img, img1: ...

  6. Kafka集群监控工具之二--Kafka Eagle

    基于kafka: kafka_2.11-0.11.0.0.tgz kafka-eagle-bin-1.2.1.tar.gz 1.下载解压 tar -zxvf kafka-eagle-bin-1.2.1 ...

  7. Qt中layout()->setSizeConstraint(QLayout::SetFixedSize);崩溃的问题

    编译环境: win764位,vs2008编译器,cbd调试器,qt4.8 背景: 按照<C++ Gui Qt4编程>书中第二章的一个例子(sortDialog)一步步抄完,编译运行,显示不 ...

  8. Linux vmstat命令实际操作介绍

    vmstat是Virtual Meomory Statistics(虚拟内存统计)的缩写,可对操作系统的虚拟内存.进程.CPU活动进行监控.它能够对系统的整体情况进行统计,无法对某个进程进行深入分析. ...

  9. Linux下Tomcat端口、进程以及防火墙设置

     Linux下Tomcat端口.进程以及防火墙设置 1,查看tomcat进程: #ps -aux | grep tomcat(或者ps -ef | grep tomcat都行) 可以看到现在运行着两个 ...

  10. 简单地说, cpp中的纯虚函数就是抽象类的具体实现

    简单地说, cpp中的纯虚函数就是抽象类的具体实现.包含了纯虚函数的类就是抽象类.