105. Construct Binary Tree from Preorder and Inorder Traversal
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的更多相关文章
- 【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 ...
- [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 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 ...
- 【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 ...
- 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
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...
- (二叉树 递归) 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 由前序和中序遍历建立二叉树 C++
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
随机推荐
- 使用oss批量上传图片
<?php set_time_limit(0);// 引入自动加载类// 确保路径是否正确require_once 'autoload.php';// 确定参数 需要申请$accessKeyId ...
- javabean实现serializable有什么用?为什么数据库持久就Bean实现这个接口?
Java的"对象序列化"能让你将一个实现了Serializable接口的对象转换成一组byte,这样日后要用这个对象时候,你就能把这些byte数据恢复出来,并据此重新构建那个对象了 ...
- c 函数及指针学习 6
不完整声明 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 /* 方法一 */ struct tag_a{ ...
- 安装sklearn时出现 "ImportError: DLL load failed" 的解决方法
如果sklearn是从 http://www.lfd.uci.edu/~gohlke/pythonlibs/#scikit-learn 中下的whl包装的 必须装他家的numpy+MKL库.如果你装的 ...
- 设置Eclipse支持C++ 11
设置Eclipse支持C++ 11 两个步骤: 项目 > Properties > C/C++ Build > Setting > GCC C++ Compiler > ...
- HDU-4747 Mex(线段树区间更新)
题目大意:给一个长度为n的整数序列,定义mex(i,j)表示区间[i,j]中没有出现过的最小非负整数,求sigma(mex(i,j)),即序列中所有连续非空子区间的mex之和. 题目分析: answe ...
- centos 编译swoole
/usr/include/php/ext/pcre/php_pcre.h:45: error: expected '=', ',', ';', 'asm' or '__attribute__' bef ...
- java performance
http://www.oracle.com/technetwork/java/performance-138178.html# http://www.oracle.com/technetwork/ja ...
- Linux-获取当前正在执行脚本的绝对路径
常见的一种误区,是使用 pwd 命 令,该命令的作用是“print name of current/working directory”,这才是此命令的真实含义,当前的工作目录,这里没有任何意思说明, ...
- CentOS如何挂载硬盘
远程SSH登录上CentOS服务器后,进行如下操作 提醒:挂载操作会清空数据,请确认挂载盘无数据或者未使用 第一步:列出所有磁盘 命令: ll /dev/disk/by-path 提示:如果无法确 ...