题目描述:

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

注意:

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

示例:

给出

中序遍历 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. java rest jax-rs 漫谈

    rest是什么 REST是英文RepresentationalState Transfer 的缩写,有中文翻译为“具象状态传输”.REST 这个术语是由 RoyFielding 在他的博士论文< ...

  2. vue中使用markdown富文本,并在html页面中展示

    想给自己的后台增加一个markdown编辑器,下面记录下引用的步骤 引入组件mavon-editor 官网地址:https://github.com/hinesboy/mavonEditor // 插 ...

  3. springboot整合redis单机及集群

    一.单机配置 properties配置 #单机redis spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis.passwor ...

  4. uboot启动正常,加载内核kernel启…

    先说现象吧:uboot能够正常启动,不过在kernel启动时却出现起不了的现象,停在这里 Uncompressing Linux.................................... ...

  5. Djano + Nginx + docker配置与管理

    在配置这个服务之前,应该对docker的基本安装与使用应该很熟悉了.下面开始直接不如正题 1.让我们创建一个名为myproject的空目录,并在src名称内添加另一个文件夹.src应该包含django ...

  6. tmux上用vim时显示错行

    环境:tmux-master,xshell4,vim7.4,CentOS6.9 tmux在某些版本会出现很奇怪的显示错乱问题,特别是在做替换的时候,只要页面翻动,显示就会乱,命令行会错位显示到状态行, ...

  7. Sprite Editor

    [Sprite Editor] 在Unity3D中,一个图片可以有多种类型(如下图).对于2D游戏开发,最常用的类型就是Sprite. 下图是Sprite Texture的属性,Packing Tag ...

  8. Linux扩展根目录

    一.简介 使用linux系统的过程中,有时发现系统根目录(/)的空间不足,导致系统运行很慢,针对该现象,本文详细介绍根目录(/)的空间扩展方法.   二.操作步骤 1)查看根目录大小 df 2)查找系 ...

  9. this与$(this)的区别

    this,表示当前的上下文对象是一个html对象,可以调用html对象所拥有的属性和方法. $(this),代表的上下文对象是一个jquery的上下文对象,可以调用jQuery的方法和属性值.

  10. 解决Tomcat错误信息:No 'Access-Control-Allow-Origin' header is present on the requested resource | Solving Tomcat Error: No 'Access-Control-Allow-Origin' header is present on the requested resource

    最近在使用GeoServer调用Vector Tile服务时,经常会显示不出来结果.打开浏览器调试台,发现报No 'Access-Control-Allow-Origin' header is pre ...