重建二叉树(根据 前序 和 中序二叉树)
2015-07-24 23:06
447 查看
下面是源代码:
#include <stdio.h> 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 = NULL; root->m_pRight = NULL; //递归结束条件,当只剩节点时 if(startPreorder == endPreorder) { if(startInorder == endInorder && *startPreorder == *startInorder) { return root; }else { return NULL; } } int *rootInorder = startInorder; while(rootInorder <= endInorder && *rootInorder != rootValue) { rootInorder++; } if(rootInorder > endInorder) { printf("无效的输入\n"); } int leftLength = rootInorder - startInorder; int rightLength = endInorder - rootInorder; if(leftLength > 0) { root->m_pLeft = ConstructCore(startPreorder + 1, startPreorder + leftLength, startInorder, startInorder + leftLength - 1); } if(rightLength > 0) { root->m_pRight = ConstructCore(startPreorder + leftLength + 1, endPreorder, rootInorder + 1, endInorder); } return root; } 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); } 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 length = 8; int preorder[length] = {1, 2, 4, 7, 3, 5, 6, 8}; int inorder[length] = {4, 7, 2, 1, 5, 3, 8, 6}; BinaryTreeNode *root = Construct(preorder, inorder, length); PrintTree(root); DestroyTree(root); }
运行结果:
注:本题用递归解,首先得分析好每次递归做的相同事情是什么,其次,判断终止条件;最后,注意边界条件和异常处理。
递归虽然简洁,但它同时也有显著的缺点。
1、递归由于是函数调用自身,而函数调用是有时间和空间消耗的:每一次函数调用,都需要在内存栈中分配空间以保存参数、返回地址及临时变量,而且往栈里面压入数据和弹出数据都需要时间。这就不难理解递归效率不如非递归。
2、递归可能会引起严重的问题:调用栈溢出。前面分析中提到徐哎哟为每一次函数调用在内存栈中分配空间,而每个进程的栈的容量是有限的。递归调用的层级太多时,就会超出栈的容量,从而导致调用栈溢出。
参考:
剑指offer
http://blog.csdn.net/zhaojinjia/article/details/9305251 http://blog.csdn.net/ljf913/article/details/8576771
相关文章推荐
- Android事件分发机制完全解析,带你从源码的角度彻底理解(下)
- 记录我的VHDL之路(三)
- mysql中文乱码,修改配置为utf-8
- 暑假规划及小学期总结
- Android事件分发机制完全解析,带你从源码的角度彻底理解(上)
- 修改UITextfield的Placeholder字体的颜色
- C++ 把输出结果写入文件/从文件中读取数据
- NoSQL 数据建模技术
- Android开发环境配置问题
- 栈与队列
- 求任意一个数字的各位数字之和
- python的数据类型
- CodeSmith 代码具体操作步骤
- 大数乘法
- Oracle EBS(ERP)行业分工(写给猎头)
- hdoj 1002 大数加法问题
- LeetCode之Decode Ways
- ios autolayerout
- Using MySQL as a NoSQL
- iOS开发——UI进阶篇(四)tableView的全局刷新,局部刷新,左滑操作,左滑出现更多按钮,进入编辑模式,批量删除,自定义批量删除