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的更多相关文章

  1. 105. Construct Binary Tree from Inorder and preorder Traversal

    /* * 105. Construct Binary Tree from Inorder and preorder Traversal * 11.20 By Mingyang * 千万不要以为root ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 【题解二连发】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 ...

  7. leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal &amp; Construct Binary Tree f

    1.  Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder travers ...

  8. Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...

  9. LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告

    Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...

随机推荐

  1. SelectDirectory使用方法以及EnableTaskWindows

    SelectDirectory使用方法 格式 Delphi syntax: On Windows: function SelectDirectory(const Caption: string; co ...

  2. 灵活运用Zend框架

    $aAwardMem = $this->dao_raward->getAwardAndMem($where,'award_level asc',false,false,false,'awa ...

  3. Oracle_系统和对象权限管理

    授予系统权限: GRANT { system_privilege | role } [,{ system_privilege | role }]... ... TO {user | role | PU ...

  4. Jimmy Choo_百度百科

    Jimmy Choo_百度百科 Jimmy Choo

  5. swig模板下拉框应用

    <div class="form-group"> <label><span class="fa fa-asterisk red"& ...

  6. POJ 3111 K Best(最大化平均值)

    题目链接:click here~~ [题目大意]有n个物品的重量和价值各自是Wi和Vi.从中选出K个物品使得单位重量的价值最大,输出物品的编号 [解题思路]:最大化平均值的经典.參见click her ...

  7. hdu-5082

    题意非常easy,就是给出父母的名字,然后依据父母的名字来给孩纸取名字! 能够将此题简化为: 孩纸的名字=父亲的frist name+字符串(_small_)+母亲额frist name; 然后将孩纸 ...

  8. javascript中算术运算符规则

    javascript中提供了几种算术运算符,+(加)  -(减)  *(乘)  /(除)  %(余),常规用法与数学上的一致:  但还规定一些特殊规则: 注:JavaScript中保存数值的方式,可以 ...

  9. js简繁转换,两种实现方式,妥妥的~

    不知道繁简按钮有什么卵用,大陆人自带繁简转换开关的好么,但是还是项目要求,做了这么一个功能,现在整理给大家用~完美兼容~么么哒 按钮的样式可以随便用css定义,注意id不要变,注意jq的事件绑定好就o ...

  10. Android UI高级交互设计Demo

    首先:是google的新标准 Google Material design 开源项目 1.直接拿来用!十大Material Design开源项目 2.收集android上开源的酷炫的交互动画和视觉效果 ...