Construct Binary Tree From Inorder and Preorder/Postorder Traversal
map<int, int> mapIndex;
void mapToIndex(int inorder[], int n)
{
for (int i = ; i < n; i++)
{
mapIndex.insert(map<int, int>::value_type(inorder[i], i);
}
} Node* buildInorderPreorder(int in[], int pre[], int n, int offset)
{
assert(n >= );
if (n == )
return NULL;
int rootVal = pre[];
int i = mapIndex[rootVal] - offset;
Node* root = new Node(rootVal);
root->left = buildInorderPreorder(in, pre+, i, offset);
root->right = buildInorderPreorder(in+i+, pre+i+, n-i-, offset+i+);
return root;
} Node* buildInorderPostorder(int in[], int post[], int n, int offset)
{
assert(n >= );
if (n == )
return NULL;
int rootVal = post[n-];
int i = mapIndex[rootVal] - offset;
Node* root = new Node(rootVal);
root->left = buildInorderPostorder(in, post, i, offset);
root->right = buildINorderPostorder(in+i+, post+i, n-i-, offset+i+);
return root;
}
Construct Binary Tree From Inorder and Preorder/Postorder Traversal的更多相关文章
- 105. Construct Binary Tree from Inorder and preorder Traversal
/* * 105. Construct Binary Tree from Inorder and preorder Traversal * 11.20 By Mingyang * 千万不要以为root ...
- Leetcode | Construct Binary Tree from Inorder and (Preorder or Postorder) Traversal
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- Leetcode, construct binary tree from inorder and post order traversal
Sept. 13, 2015 Spent more than a few hours to work on the leetcode problem, and my favorite blogs ab ...
- 36. Construct Binary Tree from Inorder and Postorder Traversal && Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal OJ: https://oj.leetcode.com/problems/cons ...
- LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal
LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...
- 【题解二连发】Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree from Preorder and Inorder Traversal
LeetCode 原题链接 Construct Binary Tree from Inorder and Postorder Traversal - LeetCode Construct Binary ...
- leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree f
1. Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder travers ...
- Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
随机推荐
- Unix/Linux环境C编程入门教程(4) Debian Linux环境搭建
Unix/Linux版本众多,我们推荐Unix/Linux初学者选用几款典型的Unix/Linux操作系统进行学习. 1.广义的Debian是指一个致力于创建自由操作系统的合作组织及其作品,由于Deb ...
- linux下修改.bash_profile立即生效的三种方法
1 . .bash_profile 2 source .bash_profile 3 exec bash --login
- 解决Node.js调用fs.renameSync报错的问题(Error: EXDEV, cross-device link not permitted)
2014-08-23 今天开始学习Node.js,在写一个文件上传的功能时候,调用fs.renameSync方法错误 出错代码所在如下: function upload(response,reques ...
- oracle字符集查看修改
一.什么是Oracle字符集 Oracle字符集是一个字节数据的解释的符号集合,有大小之分,有相互的包容关系.ORACLE 支持国家语言的体系结构允许你使用本地化语言来存储,处理,检索数据.它使数据库 ...
- 【HTML5】DOMContentLoaded事件
这个事件是从HTML中的onLoad的延伸而来的,当一个页面完成加载时,初始化脚本的方法是使用load事件,但这个类函数的缺点是仅在所有资源都完全加载后才被触发,这有时会导致比较严重的延迟,开发人员随 ...
- S3C6410嵌入式应用平台构建(二)
[2014-4/11~4/14]经过之前的实验,对Uboot已经有了大体的了解,前我们已经把led灯给点亮,但这不是我们的根本目的,我们是要进入boot启动,经过两天的分析代码和反复的实验,终于可以进 ...
- delphi “Invalid floating point operation.”错误的解决方法
这两天用webbrower写东西,有时候打开SSL加密站点时会出现”Invalid floating point operation.”的错误,上网搜了下,把解决方法贴上. 导致原因 在Delphi2 ...
- mysql中判断表中是否存在某条记录
SELECT CASE WHEN EXISTS (SELECT * FROM usergroupmap WHERE groupId = groupIdIn AND userId = v_friendI ...
- js调用ASP.NET打印代码
第一步:添加下面的js <script type="text/javascript"> function printsetup() { ...
- hdu 4034 Graph floyd
题目链接 给出一个有向图各个点之间的最短距离, 求出这个有向图最少有几条边, 如果无法构成图, 输出impossible. folyd跑一遍, 如果dp[i][j] == dp[i][k]+dp[k] ...