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. 拖放API

    拖放功能是电脑用户认为理所应当能够“顺畅运行”的功能,我们有数种方法在浏览器中启用此功能.Windows Internet Explorer 9 和早期版本的 Windows Internet Exp ...

  2. spark_updateStateByKey

    java核心代码 JavaPairDStream<String, Integer> wordCounts = pair.updateStateByKey(new Function2< ...

  3. URAL(timus)1709 Penguin-Avia(并查集)

    Penguin-Avia Time limit: 1.0 secondMemory limit: 64 MB The Penguin-Avia airline, along with other An ...

  4. Mac下的利器们介绍

    先说说一些快捷键吧,从windows下过来还不很习惯: ctrl + 开关 关机等提示 ctrl+shift+开关 关闭显示器 cmd+option+v 相当于剪贴 cmd+tab,对于最小化了的窗口 ...

  5. C++@重载函数

    关于重载详细分析参考: http://www.cnblogs.com/skynet/archive/2010/09/05/1818636.html 内部机制涉及重载函数如何解决命名冲突,调用匹配的问题 ...

  6. Java——File(文件)

     public static void main(String[] args) { // getFile(); /* * 需求:  对指定目录进行所有内容的列出,(包含子目录中的内容) * */ ...

  7. CUDA编程

    目录: 1.什么是CUDA 2.为什么要用到CUDA 3.CUDA环境搭建 4.第一个CUDA程序 5. CUDA编程 5.1. 基本概念 5.2. 线程层次结构 5.3. 存储器层次结构 5.4. ...

  8. 重力感应操控(unity iphone)

    方案一:speed 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 public var simulateAcceleromet ...

  9. 爬虫入门---Python2和Python3的不同

    Python强大的功能使得在写爬虫的时候显得十分的简单,但是Python2和Python3在这方面有了很多区别. 本人刚入门爬虫,所以先写一点小的不同. 以爬取韩寒的一篇博客为例子: 在Python2 ...

  10. DbContextConfiguration 属性

    属性 AutoDetectChangesEnabled 获取或设置一个值,该值指示是否通过 DbContext 和相关类的方法自动调用 DetectChanges 方法. 默认值为 true. Ens ...