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 duplicates do not exist in the tree.
Show Tags
Show Similar Problems
分析
跟上一道题同样的道理。
AC代码
class Solution {
public:
template <typename Iter>
TreeNode* make(Iter in_begin, Iter in_end , Iter post_begin, Iter post_end ) {
if (post_begin == post_end || in_begin == in_end)
return NULL;
int size = post_end - post_begin;
//后序遍历最后一个节点为树的根节点
TreeNode *root = new TreeNode(*(post_begin + size-1));
//在中序遍历结果中查找根节点
Iter iter = find(in_begin, in_end, *(post_begin + size - 1));
//计算左子树个数
int count = iter - in_begin;
if (iter != in_end)
{
//则在inOrder中(0 , count-1)为左子树中序遍历结果(count+1,size-1)为右子树的中序遍历序列
//在preOrder中(0,count-1)为左子树前序遍历结果(count,size-2)为右子树前序遍历结果
root->left = make(in_begin, iter , post_begin, post_begin + count);
//构造右子树
root->right = make(iter + 1, in_end ,post_begin + count, post_begin + size - 1);
}
return root;
}
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
if (inorder.empty() || postorder.empty())
return NULL;
return make(inorder.begin(), inorder.end() ,postorder.begin(), postorder.end());
}
};
LeetCode(106) Construct Binary Tree from Inorder and Postorder Traversal的更多相关文章
- 【题解二连发】Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree from Preorder and Inorder Traversal
LeetCode 原题链接 Construct Binary Tree from Inorder and Postorder Traversal - LeetCode Construct Binary ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告
[LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- 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:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal
LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- [Leetcode Week14]Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...
- 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 ...
- 36. Construct Binary Tree from Inorder and Postorder Traversal && Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal OJ: https://oj.leetcode.com/problems/cons ...
随机推荐
- HDU4035(概率期望、树形、数学)
和ZOJ3329有些像,都是用期望列出来式子以后,为了解式子,设A[i],B[i],此题又多了C[i],然后用递推(此题是树形dp)去求得ABC,最后结果只跟ABC有关,跟列写的期望数组根本无关. 虽 ...
- hdu6314( 2018 Multi-University Training Contest 2)
bryce1010模板 http://acm.hdu.edu.cn/showproblem.php?pid=6314 ----. 又是一个数学题! 这个题使用容斥原理解决的,现场看dls推公式. 我也 ...
- ssrs 里 表头 分页后显示
1. 列组,箭头,高级 2.点击行组,静态 3. 设置静态行组 属性
- 转 DG switchover
I. Pre-Switchover Checks These steps should be completed before the switchover planned maintenance w ...
- vue-quill-editor 富文本编辑器插件介绍
Iblog项目中博文的文本编辑器采用了vue-quill-editor插件,本文将简单介绍其使用方法. 引入配置 安装模块 npm install vue-quill-editor --save in ...
- GIMP 无法设置中文的问题解决
首先按照网上说的安装了language-pack-gnome-zh-hant 参考链接:http://www.ubuntu-tw.org/modules/newbb/viewtopic.php?top ...
- MFC 消息中( WPARAM wParam,LPARAM lParam)包含信息
windows的消息具有以下两个参数: (1)字参数(wParam) (2)长参数(lParam) 字参数和长参数都是32位整数,用于提供消息的附带消息,是消息传递过程中参数的载体.附加信息的消息号取 ...
- “chm 已取消到该网页的导航”解决方案
1. 右键单击该 CHM 文件,然后单击“属性”. 2. 单击“取消阻止”或者“解除锁定”. 3. 双击此 .chm 文件以打开此文件.
- 洛谷 P2292 [HNOI2004]L语言
题目描述 标点符号的出现晚于文字的出现,所以以前的语言都是没有标点的.现在你要处理的就是一段没有标点的文章. 一段文章T是由若干小写字母构成.一个单词W也是由若干小写字母构成.一个字典D是若干个单词的 ...
- spring 常用问题汇总
2018.02.06 SpringMVC中StringHttpMessageConverter乱码处理 http://blog.csdn.net/wangyangbto/article/details ...