题目描述:

根据一棵树的中序遍历与后序遍历构造二叉树。

注意:

你可以假设树中没有重复的元素。

示例:

给出

中序遍历 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)的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

  4. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告

    [LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...

  5. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...

  6. [Leetcode Week14]Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...

  7. 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 ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. Linux MTD系统剖析

    MTD,Memory Technology Device即内存技术设备,在Linux内核中,引入MTD层为NOR FLASH和NAND FLASH设备提供统一接口.MTD将文件系统与底层FLASH存储 ...

  2. centos 系统使用verdaccio搭建npm私库

    .安装nodejs yum install -y nodejs 2.安装verdaccio npm install -g verdaccio --unsafe-perm 3.配置 a.修改配置文件 c ...

  3. 第八章 数据库连接JDBC(待续)

    ············

  4. L1,L2范数和正则化 到lasso ridge regression

    一.范数 L1.L2这种在机器学习方面叫做正则化,统计学领域的人喊她惩罚项,数学界会喊她范数. L0范数  表示向量xx中非零元素的个数. L1范数  表示向量中非零元素的绝对值之和. L2范数  表 ...

  5. 微信小程序之 wx.getUserInfo引导用户授权问题

    首先,在page外定义一个函数用户判断是否为空对象 var isEmptyObject = function (e) { var temp; for (temp in e) ; } 然后,在page中 ...

  6. HTML5 使用sessionStorage实现页面返回刷新

    需求:在某个列表页面跳转到增加新项目页面后需要返回到前一个页面 并且数据最新数据.刚开始是做法是 history.back();方法 返回后页面不会自动刷新的.在新的页面重新访问之前页面的链接可以访问 ...

  7. leetcode690

    class Solution { public: int getImportance(vector<Employee*> employees, int id) { ; map<int ...

  8. Tornado 高并发源码分析之二---Tornado启动和请求处理流程

    Tornado 服务器启动流程 因为Tornado 里使用了很多传类的方式,也就是delegate,之所以要这么做,其实和 iOS 开发那样,也很多的 delegate, 如此来实现高度解耦,但是比较 ...

  9. python:if 语句的使用方法

    if-else类型: #if-else num = int(input("输入成绩!")) if num > 60: print ("及格") else: ...

  10. 如何上传网站程序(文件浏览器上传网页、FileZilla上传网站程序)

    问题场景: 网页制作完成后,程序需上传至虚拟主机. 注意事项: Windows系统的主机请将全部网页文件直接上传到FTP根目录,即 / . Linux系统的主机请将全部网页文件直接上传到 /htdoc ...