leetcode 106. 从中序与后序遍历序列构造二叉树(Construct Binary Tree from Inorder and Postorder Traversal)
题目描述:
根据一棵树的中序遍历与后序遍历构造二叉树。
注意:
你可以假设树中没有重复的元素。
示例:
给出
中序遍历 inorder = [9,3,15,20,7]
后序遍历 postorder = [9,15,7,20,3]
返回如下的二叉树:
3
/ \
9 20
/ \
15 7
解法:
# define PR pair<int, int>
/**
* 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:
int binary_search(vector<PR>& lst, int target){
int l = 0, r = lst.size() -1;
int mid = 0;
while(l <= r){
mid = l + (r-l)/2;
if(lst[mid].first < target){
l = mid + 1;
}else if(lst[mid].first == target){
return lst[mid].second;
}else{
r = mid - 1;
}
}
return -1;
}
// method 3: accepted
TreeNode* buildTree(vector<int>& postorder, vector<int>& inorder, int pl, int pr, int il, int ir, vector<PR>& inlst) {
if(pl > pr){
return NULL;
}else if(pl == pr){
return new TreeNode(postorder[pr]);
}else{
TreeNode* root = new TreeNode(postorder[pr]);
int mid = binary_search(inlst, postorder[pr]);
int lsz = mid - il;
// int rsz = ir - mid;
root->left = buildTree(postorder, inorder, pl, pl + lsz-1, il, il + lsz-1, inlst);
root->right = buildTree(postorder, inorder, pl+lsz, pr-1, il + lsz+1, ir, inlst);
return root;
}
}
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
// method 3:
int sz = postorder.size();
int pl = 0, pr = sz-1;
int il = 0, ir = sz-1;
vector<PR> inlst;
for(int i = 0; i < sz; i++){
inlst.push_back({inorder[i], i});
}
sort(inlst.begin(), inlst.end());
return buildTree(postorder, inorder, pl, pr, il, ir, inlst);
}
};
leetcode 106. 从中序与后序遍历序列构造二叉树(Construct Binary Tree from Inorder and Postorder Traversal)的更多相关文章
- [Swift]LeetCode106. 从中序与后序遍历序列构造二叉树 | 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 ...
- [Swift]LeetCode889. 根据前序和后序遍历构造二叉树 | Construct Binary Tree from Preorder and Postorder Traversal
Return any binary tree that matches the given preorder and postorder traversals. Values in the trave ...
- [Swift]LeetCode105. 从前序与中序遍历序列构造二叉树 | 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 解题报告
[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 ...
- [Leetcode Week14]Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...
- 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 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树 C++
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 ...
随机推荐
- DOM对象与JQuery对象
在初学JAVA时,经常会迷惑,分不清DOM/JQuery对象,以及可以调用的方法. 1.什么是DOM/JQuery对象 DOM对象,即是我们用传统的方法(javascript)获得的对象: eg:va ...
- python's sixteenth day for me 员工信息表
import os user_dic = { 'username':None, 'password':None, 'login':True } flag = False name_list = ['i ...
- Three.js响应和移动物体
效果图 demo import './index.css'; // stats var stats; (function(){ stats = new Stats(); document.body.a ...
- Python遍历列表删除多个列表元素
在遍历list的时候,删除符合条件的数据,结果不符合预期 num_list = [1, 2, 2, 2, 3] print(num_list) for item in num_list: if ite ...
- 每天一个Linux命令 - 【groupadd】
[命令]:grouadd [语法]:groupadd [选项] [参数] [功能介绍]:groupadd 命令勇于创建新的工作组,新工作组的信息将被添加的系统文件中. [选项说明]: -g < ...
- springMVC第一天
这些是springMVC3.2所用到的jar包 web.xml配置 <?xml version="1.0" encoding="UTF-8"?> & ...
- JanusGraph : 图和图数据库的简介
JanusGraph:图数据库系统简介 图(graph)是<数据结构>课中第一次接触到的一个概念,它是一种用来描述现实世界中个体和个体之间网络关系的数据结构. 为了在计算机中存储图,< ...
- C#中的IEnumerator、foreach、yield
[C#中的IEnumerator.foreach.yield] 1.IEnumerator,是一个接口,它的方法如下: 2.foreach语句,在编译后会变成IEnumerator的调用: 3.yie ...
- copyWithZone详解
[copyWithZone详解] NSObject实现了-copy.+copy.+copyWithZone方法.代码如下: + (id)copy { return (id)self; } + (id) ...
- 关于CountDownLatch控制线程的执行顺序
在上一篇文章中说过使用thread.join()方法.newSingleThreadExecutor单线程池来控制线程执行顺序.在文章的末尾我提出了一种构想,可否使用经典的生产者和消费者模型来控制执行 ...