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 ...
随机推荐
- win10新增快捷键
按此键 出现位置 重要程度 联想记忆 替代 用途 +A 打开“操作中心” 右侧, ★★★★★ Action 控制面板 +S ...
- bootStrap-1
bootstrap是什么? 1.简单灵活可用于架构流行的用户界面和交互借口的Html.css/javascript工具集. 2.基于html5.css3的bootstrap,具有大量的诱人特性:友好的 ...
- Android——ListView
1.ArryAdapter: arry_adapter的layout文件: <?xml version="1.0" encoding="utf-8"?&g ...
- VPS搭建VPN(pptpd)
环境:Ubuntu Server 12.04 下载FQ程序 wget http://cdxf.yun.ftn.qq.com/ftn_handler/40ad8a2875adf1f7b5193f54a5 ...
- GDB调试器
/*this project used for gdb debug c programs*//*At first,using compile command turn out the executab ...
- eclipse常用插件在线安装地址或下载地址
本文转载自:http://my.oschina.net/bloghu/blog/198922 一,反编译插件: A.Jadclipse 1.打开eclipse增加站点:http://jadclipse ...
- C语言调试的几种方法
linux系统下,在不gdb调试的情况下,我们如何解决程序崩溃问题呢?首先想到的就是添加log日志信息,其次还有以下几种方法可以帮助我们分析存在的问题: (一)add2line 程序崩溃时会打出一些崩 ...
- Linux启动新进程的三种方法
程序中,我们有时需要启动一个新的进程,来完成其他的工作.下面介绍了三种实现方法,以及这三种方法之间的区别. 1.system函数-调用shell进程,开启新进程system函数,是通过启动shell进 ...
- easyUI之layout
此组件与easyUI中的其它组件加载的方式相同,载为class方式和js方式,但此组件更多的是用class方式,其它的用js方式更为灵活.它继承panel组件及resize组件. 包括多个<di ...
- threading模块和queue模块实现程序并发功能和消息队列
简介: 通过三个例子熟悉一下python threading模块和queue模块实现程序并发功能和消息队列. 说明:以下实验基于python2.6 基本概念 什么是进程? 拥有独立的地址空间,内存,数 ...