Return any binary tree that matches the given preorder and postorder traversals.

Values in the traversals pre and post are distinct positive integers.

Example 1:

Input: pre = [1,2,4,5,3,6,7], post = [4,5,2,6,7,3,1]
Output: [1,2,3,4,5,6,7]

Note:

  • 1 <= pre.length == post.length <= 30
  • pre[] and post[] are both permutations of 1, 2, ..., pre.length.
  • It is guaranteed an answer exists. If there exists multiple answers, you can return any of them.

Runtime: 20 ms, faster than 10.38% of C++ online submissions for Construct Binary Tree from Preorder and Postorder Traversal.

想用map优化查找left root的速度,结果更慢了....

#include <assert.h>
class Solution {
private:
unordered_map<int,int>mp;
public:
TreeNode* constructFromPrePost(vector<int>& pre, vector<int>& post) {
for(int i=; i<post.size(); i++) mp[post[i]] = i;
return helper(pre, post, , pre.size()-, , post.size()-);
}
TreeNode* helper(vector<int>& pre, vector<int>& post, int pres, int pree, int poss, int pose){
if(pres > pree || poss > pose) return nullptr;
assert(pre[pres] == post[pose]);
int rootval = pre[pres];
TreeNode* root = new TreeNode(rootval);
if(pres == pree && poss == pose) return root;
int leftidx = mp[pre[pres+]];
int leftlength = leftidx - poss + ;
int leftpose = pres + leftlength;
root->left = helper(pre, post, pres+, leftpose, poss, leftidx);
root->right = helper(pre, post, leftpose+, pree, leftidx+, pose-);
return root;
}
};

LC 889. Construct Binary Tree from Preorder and Postorder Traversal的更多相关文章

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

  2. LeetCode 889. Construct Binary Tree from Preorder and Postorder Traversal

    原题链接在这里:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal/ 题 ...

  3. 【LeetCode】889. Construct Binary Tree from Preorder and Postorder Traversal 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. (二叉树 递归) 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 ...

  5. [LC] 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 ...

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

  7. [LC] 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 ...

  8. [LeetCode] 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 ...

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

随机推荐

  1. python中F/f表达式优于format()表达式

    F/f表达式可以解析任意类型的数据 具体实现,看下面示例: 1.解析变量 1 a = 10 3 b = 20 5 res1 = F"a+b的值:{a+b}" 7 print(res ...

  2. kubernetes之NFS动态提供Kubernetes后端存储卷

    StorageClass作为对存储资源的抽象定义, 对用户设置的NFS申请屏蔽后端存储的细节, 一方面减少了用户对于存储资源细节的关注, 另一方面减轻了管理员手工管理pv的工作, 由系统自动完成pv的 ...

  3. except用法

    #!/usr/bin/expect set timeout 20 spawn ssh -l root 172.25.254.102 expect "(yes/no)?" send ...

  4. 6.Shell 计划任务服务程序

    计划任务服务程序 经验丰富的系统运维工程师可以使得Linux在无需人为介入的情况下,在指定的时间段自动启用或停止某些服务或命令,从而实现运维的自动化. 如何设置服务器的计划任务服务,把周期性.规律性的 ...

  5. Redis08-击穿&穿透&雪崩&spring data redis

    一.常见概念 击穿: 概念:redis作为缓存,设置了key的过期时间,key在过期的时候刚好出现并发访问,直接击穿redis,访问数据库 解决方案:使用setnx() ->相当于一把锁,设置的 ...

  6. Beta冲刺版本第一天

    该作业所属课程:https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass2 作业要求地址:https://edu.cnblogs.com ...

  7. 如何保存ActionMailbox inbound HTML email和关于ActionText与ActiveStorage的附加

    gi代码: https://github.com/gorails-screencasts/action-mailbox-action-text/commit/3aeedc09441696c9489ed ...

  8. 怎么卸载hexo

    npm uninstall hexo -g 卸载失败 npm uninstall hexo-cli -g 推荐这个,成功卸载

  9. UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd0 in position 0: invalid continuation byte

    用pandas打开csv文件可能会出现这种情况,原因可能是excel自己新建一个*.csv文件时候容易出错.进入文件另存为,然后选择csv文件即可.

  10. 小程序swiper组件的bindchange方法重复执行问题

    这是官方文档的说法给出了swiper组件一直来回滑动的bug原因 以下是修正方法 <swiper autoplay="{{autoplay}}" interval=" ...