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< ...
随机推荐
- PostgreSQL学习(1)-- 安装pageinspect extension
1.源码编译 pageinspect的源码在postgre源码包的contrib目录下,解压postgre源码包后进入对应的目录. [root@localhost pageinspect]# pwd ...
- ccf 201712-2 游戏(Python实现)
一.原题 问题描述 试题编号: 201712-2 试题名称: 游戏 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 有n个小朋友围成一圈玩游戏,小朋友从1至n编号,2号小朋友坐 ...
- 22.Yii2.0框架多表关联一对一查询之hasOne
思路: 通过文章查它对应的分类信息 一对一的关系 控制器里 //一对一关联查询 public function actionRelatesone() { //方法一,hasOne() 用查一条文章的结 ...
- Applied Nonparametric Statistics-lec3
Ref: https://onlinecourses.science.psu.edu/stat464/print/book/export/html/4 使用非参数方法的优势: 1. 对总体分布做的假设 ...
- leetcode-6-basic
解题思路: 这道题真实地反映了我今晚有多脑残=.=只需要从根号N开始向前找,第一个能被N整除的数就是width,然后存到结果就 可以了.因为离根号N越近,width越大,与length的差越小. ve ...
- LeetCode(278)First Bad Version
题目 You are a product manager and currently leading a team to develop a new product. Unfortunately, t ...
- stm32L0工程建立(HAL+IAR,无cubemx)
https://files.cnblogs.com/files/CodeWorkerLiMing/STM32HAL%E5%BA%93%E5%AD%A6%E4%B9%A0%E2%80%94%E5%B7% ...
- VS2017生成.net core项目报错:The current .NET SDK does not support targeting .NET Core 2.1. Either
今天在生成一个项目的时候,生成报错,错误如下:The current .NET SDK does not support targeting .NET Core 2.1. Either target ...
- js 判断ie的版本号
//IE6判断: var isIE6 = !!window.ActiveXObject && !window.XMLHttpRequest; //或者: if(navigator.us ...
- SDOJ 3740 Graph
8.9 t3 [描述] 给你一个图,一共有 N 个点,2*N-2 条有向边. 边目录按两部分给出 1. 开始的 n-1 条边描述了一颗以 1 号点为根的生成树,即每个点都可以由 1 号点 到达. 2. ...