剑指offer面试题6:重建二叉树
来源:互联网 发布:百度云盘 linux客户端 编辑:程序博客网 时间:2024/06/11 15:20
#include "iostream"#include <string>#include "stack"using namespace std;struct BinaryTreeNode{int data;BinaryTreeNode *left;BinaryTreeNode *right;};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){int rootValue = startPreorder[0];BinaryTreeNode *root = new BinaryTreeNode();root->data = rootValue;root->left = root->right = NULL;if (startPreorder==endPreorder){if (startInorder == endInorder&&*startInorder == *startPreorder)return root;elsethrow exception("Invalid input");}int *rootInorder = startInorder;while (rootInorder <= endInorder&&*rootInorder != rootValue)++rootInorder;if (rootInorder == endInorder&&*rootInorder != rootValue)throw exception("Invalid input");int leftLength = rootInorder -startInorder;int* leftPreorderEnd = startPreorder + leftLength;if (leftLength>0){//leftroot->left = constructCore(startPreorder + 1, leftPreorderEnd, startInorder, rootInorder - 1);}if (leftLength < endPreorder - startPreorder){//rightroot->right = constructCore(leftPreorderEnd + 1, endPreorder, rootInorder + 1, endInorder);}return root;}int main(){system("pause");return 0;}
0 0
- 《剑指Offer》面试题-重建二叉树
- 剑指offer--面试题6: 重建二叉树(树)
- 剑指offer-->面试题6 重建二叉树
- 【剑指offer】面试题6:重建二叉树
- 《剑指offer》面试题6:重建二叉树
- 剑指offer 面试题6 重建二叉树
- 剑指Offer:面试题6 重建二叉树
- 《剑指offer》面试题6:重建二叉树
- 《剑指offer》面试题6重建二叉树
- 剑指offer面试题6--重建二叉树
- 理解《剑指Offer》之面试题6 重建二叉树
- 剑指offer 面试题6 重建二叉树
- 剑指offer面试题6 重建二叉树(c)
- 剑指offer面试题6 重建二叉树(java)
- 《剑指Offer》面试题6:重建二叉树
- 剑指offer--面试题6:重建二叉树--Java实现
- 【剑指Offer学习】【面试题6 :重建二叉树】
- 《剑指Offer》学习笔记--面试题6:重建二叉树
- php socket服务端和客户端实例
- POJ 1442 Treap模板
- 点石成金-读书笔记
- Android系统中Parcelable和Serializable的区
- NYOJ三点顺序
- 剑指offer面试题6:重建二叉树
- 如何把文件夹内文件名批量导出到txt生成清单
- Windows Server 2003 报错:”NTDETECT 失败“ 和 “文件或目录损坏且无法读取,请运行chkdsk工具”
- 297. Serialize and Deserialize Binary Tree
- C#控制台 0-99随机数产生
- 第二天
- Struts2的异常处理
- 关于UITableView中Cell的保持/保存 选中状态的简单方法
- Cocos2d-x Lua基本操作