题目:由前序、中序遍历序列重建二叉树

虽然思路能想到,但是实际写却无从下手。。。
下面重现作者代码,多多实践。。。

#include<exception>

//首先定义二叉树节点
struct BinaryTreeNode
{
int bt_Value;
BinaryTreeNode* bt_pLeft;
BinaryTreeNode* bt_pRight;
}; //核心函数:ConstructCore(int* StartPreOrder, int* EndPreOrder, int* StartInOrder, int* EndInOrder);
BinaryTreeNode* ConstructCore(int* StartPreOrder, int* EndPreOrder,
int* StartInOrder, int* EndInOrder)
//StartPreOrder,EndPreOrder为先序遍历序列的首尾;StartInOrder,EndInOrder为中序遍历序列的首尾
//总体思路:1、先序遍历首元素即为根节点,创建根节点;
// 2、中序遍历中寻找根节点,以此为界,前面为左子树,后面为右子树;
// 3、函数递归以创建左子树和右子树;
// 递归终止条件:StartPreOrder == EndPreOrder时,StartInOrder同时应等于EndInOrder,为叶子节点。 //因Construct函数中已验证了各个指针项的合法性,所以ConstructCore不再验证。
{
//根据前序序列首元素创建根节点
int RootValue = StartPreOrder[];
BinaryTreeNode* root = new BinaryTreeNode;
root->bt_Value = RootValue;
root->bt_pLeft = root->bt_pRight = NULL; //递归终止条件
if(StartPreOrder == EndPreOrder)
{
if(StartInOrder == EndInOrder && *StartPreOrder == *StartInOrder)
return root; //该节点为叶子节点,创建子树结束
else
throw std::exception("Invalid Input!");
} //在中序序列中寻找根节点位置
int* rootInOrder = StartInOrder;
while(rootInOrder <= EndInOrder && *rootInOrder != RootValue)
rootInOrder++;
if(rootInOrder > EndInOrder)
throw std::exception("Invalid Input!");
//确定左子树的节点个数
int LeftTreeLength = rootInOrder - StartInOrder;
int* LeftTreePreOrderEnd = StartPreOrder + LeftTreeLength;
//创建左子树
if(LeftTreeLength > )
root->bt_pLeft = ConstructCore(StartPreOrder+,LeftTreePreOrderEnd,StartInOrder,rootInOrder-);
//创建右子树
if(rootInOrder < EndInOrder)
root->bt_pRight = ConstructCore(LeftTreePreOrderEnd+,EndPreOrder,rootInOrder+,EndInOrder); return root;
} //调用该递归函数的Construct函数如下:
BinaryTreeNode* Construct(int* PreOrder, int* InOrder, int length)
{
if(PreOrder == NULL || InOrder == NULL || length <= )
return NULL;
return ConstructCore(PreOrder,PreOrder+length-,InOrder,InOrder+length-);
}
//BinaryTree.cpp中的各个接口实现
//1. 创建二叉树节点
BinaryTreeNode* CreateBinaryTreeNode(int value)
{
BinaryTreeNode* pNode = new BinaryTreeNode;
pNode->bt_Value = value;
pNode->bt_pLeft = pNode->bt_pRight = NULL; return pNode;
} //2. 连接树节点
void ConnectBinaryTreeNodes(BinaryTreeNode* pParent, BinaryTreeNode* pLeft, BinaryTreeNode* pRight)
{
if(pParent != NULL)
{
pParent->bt_pLeft = pLeft;
pParent->bt_pRight = pRight;
}
else
return;
} //3. 打印树节点及其孩子节点
void PrintTreeNode(BinaryTreeNode* pNode)
{
if(pNode != NULL)
{
cout<<"The value of this node is: "<<pNode->bt_Value
<<endl;
if(pNode->bt_pLeft != NULL)
cout<<"The value of the left-child node is: "<<pNode->bt_pLeft->bt_Value
<<endl;
else
cout<<"The left-child node is NULL!"<<endl;
if(pNode->bt_pRight != NULL)
cout<<"The value of the right-child node is: "<<pNode->bt_pRight->bt_Value
<<endl;
else
cout<<"The right-child node is NULL!"<<endl;
}
else
cout<<"This node is NULL!"<<endl;
} //4. 打印树 (逐三角打印)
void PrintTree(BinaryTreeNode* pRoot)
{
PrintTreeNode(pRoot); if(pRoot != NULL)
{
if(pRoot->bt_pLeft != NULL)
PrintTree(pRoot->bt_pLeft);
if(pRoot->bt_pRight != NULL)
PrintTree(pRoot->bt_pRight);
} } //5. 销毁树
void DestroyTree(BinaryTreeNode* pRoot)
{
if(pRoot != NULL)
{
BinaryTreeNode* pLeft = pRoot->bt_pLeft;
BinaryTreeNode* pRight = pRoot->bt_pRight; delete pRoot;
pRoot = NULL; DestroyTree(pLeft);
DestroyTree(pRight);
}
}

剑指offer--面试题6的更多相关文章

  1. 剑指Offer:面试题15——链表中倒数第k个结点(java实现)

    问题描述 输入一个链表,输出该链表中倒数第k个结点.(尾结点是倒数第一个) 结点定义如下: public class ListNode { int val; ListNode next = null; ...

  2. 剑指offer面试题3 二维数组中的查找(c)

    剑指offer面试题三:

  3. 剑指Offer——笔试题+知识点总结

    剑指Offer--笔试题+知识点总结 情景回顾 时间:2016.9.23 12:00-14:00 19:00-21:00 地点:山东省网络环境智能计算技术重点实验室 事件:笔试 注意事项:要有大局观, ...

  4. C++版 - 剑指offer之面试题37:两个链表的第一个公共结点[LeetCode 160] 解题报告

    剑指offer之面试题37 两个链表的第一个公共结点 提交网址: http://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46?t ...

  5. C++版 - 剑指offer 面试题23:从上往下打印二叉树(二叉树的层次遍历BFS) 题解

    剑指offer  面试题23:从上往下打印二叉树 参与人数:4853  时间限制:1秒  空间限制:32768K 提交网址: http://www.nowcoder.com/practice/7fe2 ...

  6. C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解

    剑指offer 面试题39:判断平衡二叉树 提交网址:  http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...

  7. Leetcode - 剑指offer 面试题29:数组中出现次数超过一半的数字及其变形(腾讯2015秋招 编程题4)

    剑指offer 面试题29:数组中出现次数超过一半的数字 提交网址: http://www.nowcoder.com/practice/e8a1b01a2df14cb2b228b30ee6a92163 ...

  8. C++版 - 剑指Offer 面试题39:二叉树的深度(高度)(二叉树深度优先遍历dfs的应用) 题解

    剑指Offer 面试题39:二叉树的深度(高度) 题目:输入一棵二叉树的根结点,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度.例如:输入二叉树 ...

  9. C++版 - 剑指offer 面试题24:二叉搜索树BST的后序遍历序列(的判断) 题解

    剑指offer 面试题24:二叉搜索树的后序遍历序列(的判断) 题目:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则返回true.否则返回false.假设输入的数组的任意两个 ...

  10. C++版 - 剑指Offer 面试题45:圆圈中最后剩下的数字(约瑟夫环问题,ZOJ 1088:System Overload类似)题解

    剑指Offer 面试题45:圆圈中最后剩下的数字(约瑟夫环问题) 原书题目:0, 1, - , n-1 这n个数字排成一个圈圈,从数字0开始每次从圆圏里删除第m个数字.求出这个圈圈里剩下的最后一个数字 ...

随机推荐

  1. xenserver 清理日志的方法

    转载:http://vps.gl/vps/259.html XENSERVER服务器经过半年或者一年使用后,XenServer产生了很多日志文件.XenServer默认是4G系统空间,而这些日志文件会 ...

  2. Mysql中查找并删除重复数据的方法

    (一)单个字段 1.查找表中多余的重复记录,根据(question_title)字段来判断 代码如下 复制代码 select * from questions where question_title ...

  3. DTCMS使用ajax局部刷新

    动力启航的DTCMS代码遇到的问题: 前台post请求: $.ajax({ type: "POST", url: sendUrl, dataType: "json&quo ...

  4. 解决win7 下 curl无法加载的问题

    最近分别在WIN7和Windows8 上分别安装php 高版本!都遇到了这个问题! 一.win7系统64位, apache2.2, php 5.35 vc6 版本 这个比较容易: 1. phpinfo ...

  5. 10+ powerful debugging tricks with Visual Studio

    10+ powerful debugging tricks with Visual Studio Original link : http://www.codeproject.com/Articles ...

  6. gcc常用命令介绍

    GCC 全称是 GNU C Compiler,是gnu中最流行的c & c++编译器,下面我们看一下一些主要的参数使用方法. 对于一个源文件可以直接生成可执行文件 gcc test.c 默认生 ...

  7. Regionals 2013 :: North America - Southeast USA

    Regionals 2013 :: North America - Southeast USA It Takes a Village As a Sociologist, you are studyin ...

  8. php下载文件,添加响应头

    //下载,添加响应头信息 header('Content-type:application/octet-stream'); header('Content-Disposition:attachment ...

  9. 让backspace键默认为删除键

    在/root/.bashrc  中插入一条: stty erase ^H

  10. 2013-07-29 IT 要闻速记快想

    ### ========================= ###传动视暴雪82亿美元赎身,腾讯参与投资 ### ========================= ###帮助企业解决打印&邮 ...