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 ...
随机推荐
- flex4 日期类型字符串转日期类型(string转Date)(转)
mysql数据库中存储的日期类型通过PHP返回到flex端为字符串类型,这样在flex中进行处理时就必须要将字符串转化为Date类型.如果仅仅是 "年/月/日" 的组合,而没有涉及 ...
- java文件读写操作
Java IO系统里读写文件使用Reader和Writer两个抽象类,Reader中read()和close()方法都是抽象方法.Writer中 write(),flush()和close()方法为抽 ...
- uva 10129 poj 1386 hdu 1116 zoj 2016 play on words
//本来是想练一下欧拉回路的,结果紫书上那题是大水题!!!!! 题意:给出n个单词,是否可以把单词排列成每个单词的第一个字母和上一个单词的最后一个字母相同 解:欧拉通路存在=底图联通+初度!=入度的点 ...
- New Relic——手机应用app开发达人的福利立即就到啦!
HiWork集成的第三方服务(机器人)将有新的添加啦,添加了BitBucket和New Relic.分别做下介绍啦! 1.BitBucket BitBucket 是一家源码托管站点.採用Mercuri ...
- IBM中枪后,下一个是谁?
冯强/文 在之前的博文<信息战第二弹:中国对美国咨询公司Say no>我以前提到对美国咨询服务公司在国企开展业务的限制.有可能波及IBM.Microsoft.Google.CISCO.Or ...
- ASP.NET导出EXCEl方法使用COM.EXCEL不使用EXCEl对象
第一种:导出gridVIEW中的数据,用hansTABLE做离线表,将数据库中指定表中的所有数据按GRIDVIEW中绑定的ID导出 只能导出数据不能去操作相应的EXCEl表格,不能对EXCEL中的数据 ...
- su Authentication failure解决
su Authentication failure解决 关于Ubuntu桌面系统su root时认证失败的问题 1. Ubuntu 默认没有给root用户设置密码,当我们su root命令时, 提示认 ...
- 在zendstudio上配置SVN
本文介绍zendstudio结合SVN的使用方法. 一.部署svn服务器 直接安装用svn软件,配置太过麻烦,用户和版本库.权限管理不太方便.推荐使用CollabNetSubversionEdge.以 ...
- 5.7.2.4 random() 方法
Math.random()方法返回大于等于0小于1的一个随机数.对于某些站点来说,这个方法非常实用,因为可以利用它来随机显示一些名人名言和新闻事件.套用下面的公式,就可以利用Math.random() ...
- 使用mybatis查询数据,按特定顺序排序
有如下表table_people id name 1 dwyane 2 james 3 paul 4 bosh ...