LeetCode OJ--Construct Binary Tree from Preorder and Inorder Traversal *
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 *的更多相关文章
- [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 ...
- (二叉树 递归) 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 【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 ...
- [leetcode] 105. Construct Binary Tree from Preorder and Inorder Traversal (Medium)
原题 题意: 根据先序和中序得到二叉树(假设无重复数字) 思路: 先手写一次转换过程,得到思路. 即从先序中遍历每个元素,(创建一个全局索引,指向当前遍历到的元素)在中序中找到该元素作为当前的root ...
- Leetcode#105 Construct Binary Tree from Preorder and Inorder Traversal
原题地址 基本二叉树操作. O[ ][ ] [ ]O[ ] 代码: TreeNode *restore(vector< ...
随机推荐
- 前端MVVM模式及其在Vue和React中的体现
MVVM相关概念 Mvvm 前端数据流框架精讲 1) MVVM典型特点是有四个概念:Model.View.ViewModel.绑定器.MVVM可以是单向绑定也可以是双向绑定甚至是不绑定 2) 绑定器: ...
- 20181207(sys,shelve,logging)
一.logging模块 logging专门用来记录日志 日志的级别分为五级,可以用数字表示,从低到高分别为: import logginglogging.info('info') #10logg ...
- drf 认证功能
drf(django rest-framework)认证组件 复习 HyperlinkedIdentityField ```python 功能:快速生成连接 1. publish = seriali ...
- 在windows7 32ibt安装MongoDB数据库的方法及连接失败解决方案
参考 https://www.cnblogs.com/cnblogs-jcy/p/6734889.html http://yunkus.com/mongodb-install-config-in-wi ...
- 自动设置IP地址bat脚本
自动获取IP及DNS: netsh interface ip set address name="本地连接" source=dhcpnetsh interface ip set d ...
- Page-Object思想
为什么要使用page-object 集中管理元素对象 集中管理一个page内的公共方法 后期维护方便 集中管理元素对象 实现方法: 调用方法: WebElement element = dri ...
- [转] immutability-helper 插件的基本使用(附源码)
概念 先理解一下 Immutable 的概念,Immutable数据就是一旦创建,就不能更改的数据.每当对Immutable对象进行修改的时候,就会返回一个新的Immutable对象,以此来保证数据的 ...
- 大数据学习——sparkSql
官网http://spark.apache.org/docs/1.6.2/sql-programming-guide.html val sc: SparkContext // An existing ...
- linux随笔一
1.过滤列表输出:*表示多个字符 ll mon* 结果输出为:mongo.sh mongo_sso.sh 2.man +命令 例如:man ls 查看命令的用法 3.cd 将shell回话移动到 ...
- Leetcode 477.汉明距离总和
汉明距离总和 两个整数的 汉明距离 指的是这两个数字的二进制数对应位不同的数量. 计算一个数组中,任意两个数之间汉明距离的总和. 示例: 输入: 4, 14, 2 输出: 6 解释: 在二进制表示中, ...