Given preorder and inorder traversal of a tree, construct the binary tree.

==============

基本功:

利用前序和中序构建二叉树

,

===

code

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
///help_buildTree_pi()
template<typename Iteratorr>
TreeNode *help_buildTree_pi(Iteratorr pre_first,Iteratorr pre_last,
Iteratorr in_first,Iteratorr in_last){
if(pre_first == pre_last) return nullptr;
if(in_first == in_last) return nullptr; auto root = new TreeNode(*pre_first);
auto inRootPos = find(in_first,in_last,*pre_first);
auto leftSize = distance(in_first,inRootPos); root->left = help_buildTree_pi(next(pre_first),next(pre_first,leftSize+),
in_first,next(in_first,leftSize));
root->right = help_buildTree_pi(next(pre_first,leftSize+),pre_last,next(inRootPos),in_last);
return root;
}
TreeNode* buildTree_pi(vector<int> &preorder,vector<int> &inorder){
return help_buildTree_pi(preorder.begin(),preorder.end(),inorder.begin(),inorder.end());
}
};

105. Construct Binary Tree from Preorder and Inorder Traversal的更多相关文章

  1. 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal

    Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...

  2. [LeetCode] 105. Construct Binary Tree from Preorder and Inorder Traversal 由先序和中序遍历建立二叉树

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  3. leetcode 105 Construct Binary Tree from Preorder and Inorder Traversal ----- java

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  4. LeetCode OJ 105. Construct Binary Tree from Preorder and Inorder Traversal

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  5. 【leetocde】 105. Construct Binary Tree from Preorder and Inorder Traversal

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  6. LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal (用先序和中序树遍历来建立二叉树)

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  7. 【一天一道LeetCode】#105. Construct Binary Tree from Preorder and Inorder Traversal

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...

  8. (二叉树 递归) leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  9. LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

随机推荐

  1. ABAP DEMO

    sap Program DEMO 介绍 Program Description BALVBT01 Example SAP program for displying multiple ALV repo ...

  2. URAL 1218 Episode N-th: The Jedi Tournament(强连通分量)(缩点)

    Episode N-th: The Jedi Tournament Time limit: 1.0 secondMemory limit: 64 MB Decided several Jedi Kni ...

  3. Ghost的相关问题

    一些和Ghost使用有关的问题,记录在这里. 1,有时候使用ghost恢复,发现最后一步选择驱动器是灰色的,这是因为备份文件有些是用Disk模式,有些使用partition模式,所有恢复的时候如果其中 ...

  4. Compute Mean Value of Train and Test Dataset of Caltech-256 dataset in matlab code

    Compute Mean Value of Train and Test Dataset of Caltech-256 dataset in matlab code clc;imPath = '/ho ...

  5. VGG_19 train_vali.prototxt file

    name: "VGG_ILSVRC_19_layer" layer {  name: "data"  type: "ImageData"  ...

  6. HTTPS-SSL/TSL与SNI的关系以及同IP多域名虚拟主机的SSL/TSL认证

    早期的SSLv2根据经典的公钥基础设施PKI(Public Key Infrastructure)设计,它默认认为:一台服务器(或者说一个IP)只会提供一个服务,所以在SSL握手时,服务器端可以确信客 ...

  7. Unity3D研究院之Inspector面板枚举的别名与排序

    虽然mono是支持unicode的.可以在枚举里写中文,但是我还是觉得写英文好一些.可是在编辑器上策划是希望看到的是中文的,还有就是枚举的展示排序功能,策划在编辑的时候为了方便希望把常用的枚举排上前面 ...

  8. 20150906VS小知识

    .sln :解决方案管理文件.caproj:项目管理文件.cs:程序源代码文件项目文件目录下有个bin文件夹,里面的debug文件夹,里面存放生成后的程序. //注释一行/* */ 注释一段 alt ...

  9. bash feature

    bash调用-启动文件-交互式shell-条件表达式-shell算术-别名-数组-目录栈-提示符控制-受限shell-posix模式 受限shell bash --restricted 它用来建立一个 ...

  10. unity 合并skinnedMeshRenderer中遇到的一个大坑

    将多个skinnedMeshRenderer合并成一个skinnedMeshRenderer,主要涉及的mesh合并.骨骼列表合并.重定向顶点骨骼索引.其中重定向顶点骨骼索引只是通过加偏值即可完成,所 ...