http://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/

根据二叉树的前序遍历和中序遍历序列求二叉树

学习:迭代器的使用和auto变量

#include <iostream>
#include <vector>
using namespace std; struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
}; class Solution{
public:
TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder)
{
return buildTree(begin(preorder),end(preorder),begin(inorder),end(inorder));
} template<typename InputIterator>
TreeNode* buildTree(InputIterator pre_first,InputIterator pre_last,InputIterator in_first,InputIterator 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 = buildTree(next(pre_first),next(pre_first,leftSize+),in_first,next(in_first,leftSize));
root->right = buildTree(next(pre_first,leftSize+),pre_last,next(inRootPos),in_last); return root;
}
}; int main()
{
Solution myS;
int arr1[] = {,,,,,, };
int arr2[] = {,,,,,, };
vector<int> inorder(arr1,arr1+) ;
vector<int> postorder(arr2 ,arr2+);
TreeNode *myNode; myNode = myS.buildTree(inorder,postorder); cout<<"hi"<<endl;
return ;
}

LeetCode OJ--Construct Binary Tree from Preorder and Inorder Traversal *的更多相关文章

  1. [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 ...

  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 (用先序和中序树遍历来建立二叉树)

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

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

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

  6. leetcode题解:Construct Binary Tree from Preorder and Inorder Traversal (根据前序和中序遍历构造二叉树)

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

  7. Java for 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 ...

  8. 【LeetCode】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 (Medium)

    原题 题意: 根据先序和中序得到二叉树(假设无重复数字) 思路: 先手写一次转换过程,得到思路. 即从先序中遍历每个元素,(创建一个全局索引,指向当前遍历到的元素)在中序中找到该元素作为当前的root ...

  10. Leetcode#105 Construct Binary Tree from Preorder and Inorder Traversal

    原题地址 基本二叉树操作. O[       ][              ] [       ]O[              ] 代码: TreeNode *restore(vector< ...

随机推荐

  1. 【NOIP2017提高A组冲刺11.8】好文章

    #include<algorithm> #include<iostream> #include<cstring> #include<cstdio> us ...

  2. java--String、StringBuilder、StringBuffer的解析和比较?

    一.String的解析 1.String的含义 ①String是不可以被继承的,String类是final类,String类是由char[]数组来存储字符串. ②String是不可变的字符序列,如果存 ...

  3. 【Python学习之五】高级特性3(切片、迭代、列表生成器、生成器、迭代器)

    3.列表生成器(List Comprehensions) 列表生成式即List Comprehensions,是Python内置的非常简单却强大的可以用来创建list的生成式.举个例子,要生成list ...

  4. CentOS 7 忘记root密码解决方法

    CentOS 7  root密码的重置方式和CentOS 6完全不一样,CentOS 7与之前的版本6变化还是比较大的,以进入单用户模式修改root密码为例: 1.重启机器,进入grub菜单的时候按e ...

  5. phpExcel使用方法二

    require_once './phpexcel/PHPExcel.php'; // 首先创建一个新的对象 PHPExcel object $objPHPExcel = new PHPExcel(); ...

  6. python学习--python 连接SQLServer数据库(两种方法)

    1. python 学习.安装教程参照: http://www.runoob.com/python/python-tutorial.html 2. 集成开发环境 JetBrains PyCharm C ...

  7. Linux删除非空目录的方法

    rmdir 无法删除非空目录. rm -rf 可以递归,强制,删除目录

  8. Jboss性能调优

    1,Jboss5调优指南 https://www.redhat.com/f/pdf/JB_JEAP5_PerformanceTuning_wp_web.pdf 1,Jboss7.1 性能调优指南 a: ...

  9. 骨牌覆盖问题总结!hihoCoder/ NYOJ-1273宣传墙1151

    本想着做一下第九届河南省省赛题,结果被这个类似骨牌覆盖的题卡住了,队友然我去hihoCoder上老老实实把骨牌覆盖一.二.三做完,这题就没什么问题了.虽然很不情愿,但还是去见识了一下.  骨牌覆盖问题 ...

  10. 九度oj 题目1456:胜利大逃亡

    题目描述: Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会.魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A个B*C的矩阵,刚开始Ignatius ...