Leetcode 之Construct Binary Tree(52)

根据先序和中序构造二叉树、根据中序和后序构造二叉树,基础题,采用递归的方式解决,两题的方法类似。需要注意的是迭代器的用法。
//先序和中序
TreeNode *buildTree(vector<int>& preorder, vector<int>& inorder)
{
return buildTree(begin(preorder), end(preorder), begin(inorder), begin(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 intRootPos = find(in_first, in_last, *pre_first);
//得到根结点的左半部分
auto leftSize = distance(in_first, intRootPos);
//递归构造,注意去掉根结点
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(intRootPos), in_last); return root;
}
//中序和后序
TreeNode *buildTree1(vector<int>& inorder, vector<int>& postorder)
{
return buildTree1(begin(inorder), end(inorder), begin(postorder), begin(postorder));
}
template<typename InputIterator>
TreeNode *buildTree1(InputIterator in_first, InputIterator in_last,
InputIterator post_first, InputIterator post_last)
{
if (in_first == in_last)return nullptr;
if (post_first == post_last)return nullptr;
//后序最后一个为根结点
const auto val = *prev(post_last)
auto root = new TreeNode(val);
//查找根结点在中序的位置,返回的是迭代器
auto intRootPos = find(in_first, in_last, val);
//得到根结点的左半部分
auto leftSize = distance(in_first, intRootPos);
//递归构造,注意去掉根结点
root->left = buildTree(in_first, intRootPos,
post_first, next(post_first, leftSize));
root->right = buildTree(next(intRootPos), in_last,
next(post_first,leftSize), prev(post_last)); return root;
}
Leetcode 之Construct Binary Tree(52)的更多相关文章
- (二叉树 递归) 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 106. Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- [LeetCode] 106. Construct Binary Tree from Postorder and Inorder Traversal_Medium tag: Tree Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- [LeetCode] 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- [Leetcode Week14]Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...
- LeetCode 889. Construct Binary Tree from Preorder and Postorder Traversal
原题链接在这里:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal/ 题 ...
- [LeetCode] 889. Construct Binary Tree from Preorder and Postorder Traversal 由先序和后序遍历建立二叉树
Return any binary tree that matches the given preorder and postorder traversals. Values in the trave ...
- [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 ...
- Java for LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 31041 Total Submissions: ...
- leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree f
1. Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder travers ...
随机推荐
- ThinkPHP中的跨控制器调用与框架执行流程
一.跨控制器调用 UserController.class.php <?php namespace Home/Controller use Think/Controller class User ...
- [工具类]获取url中参数列表
写在前面 在项目中经常用到解析url中参数的逻辑,今天先下载就自己封装了一个方法,方便以后使用的时候,信手拈来.当然这里给出的方法是针对常见的url参数类型的,对于重写url,或者路由格式的不考虑. ...
- SPOJ QTREE 树链剖分
树链剖分的第一题,易懂,注意这里是边. #include<queue> #include<stack> #include<cmath> #include<cs ...
- COLORBOX文档
1,flash覆盖colorbox: 2,colorbox在ie中的位置和行为异常: 3,colorbox的位置和行为异常(不区分浏览器): 4,用colorbox显示外部文档时显示不正确: 5,在i ...
- oracle基本语句
ALTER TABLE SCOTT.TEST RENAME TO TEST1--修改表名 ALTER TABLE SCOTT.TEST RENAME COLUMN NAME TO NAME1 --修改 ...
- ubuntu使用ssh登入不执行.bashrc解决方法
解决方法,可以直接输入 bash即可. 理解 bashrc 和 profile linux bashrc profile SEP 30TH, 2011 BY SUNTEYA 在一般的 linux 或者 ...
- 【ASP.NET Web API教程】6.1 媒体格式化器
http://www.cnblogs.com/r01cn/archive/2013/05/17/3083400.html 6.1 Media Formatters6.1 媒体格式化器 本文引自:htt ...
- shell与变量的声明的操作
1.给命令起别名:alias 执行下面命令后,可以使用dir代替ls –l 命令,显示目录中的文件详细信息: 还可以用一个别名表示几个命令 的结合: 2.ps:显示当前登录会话的所有活动进程: 3.更 ...
- Extjs Form用法详解(适用于Extjs5)
Extjs Form是一个比较常用的控件,主要用来显示和编辑数据的,今天这篇文章将介绍Extjs Form控件的详细用法,包括创建Form.添加子项.加载和更新数据.验证等. 本文的示例代码适用于Ex ...
- c++标准库和stl关系
C++标准库的所有头文件都没有扩展名.C++标准库的内容总共在50个标准头文件中定义,其中18个提供了C库的功能. <cname>形式的标准头文件[ <complex>例外]其 ...