LC 889. Construct Binary Tree from Preorder and Postorder Traversal
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[]
andpost[]
are both permutations of1, 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的更多相关文章
- [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 ...
- LeetCode 889. Construct Binary Tree from Preorder and Postorder Traversal
原题链接在这里:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal/ 题 ...
- 【LeetCode】889. Construct Binary Tree from Preorder and Postorder Traversal 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- (二叉树 递归) 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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
随机推荐
- C# 基础 字符串 集合 文件操作
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 第六章· MySQL索引管理及执行计划
一.索引介绍 1.什么是索引 1)索引就好比一本书的目录,它能让你更快的找到自己想要的内容. 2)让获取的数据更有目的性,从而提高数据库检索数据的性能. 2.索引类型介绍 1)BTREE:B+树索引 ...
- NORDIC 出现NRF_ERROR_NO_MEM错误
Which SDK version are you using, is it SDK v12.x.x? Which function returns NRF_ERROR_NO_MEM? Is it s ...
- Elasticsearch索引操作
一.索引初始化操作 插件推荐使用head.marvel (收费) 1.1 创建新索引 curl -XPUT 'http://localhost:9200/test' -d ' { "sett ...
- action mailbox
Action Mailer Basics和Action Mailbox Basics:邮件系统. https://edgeguides.rubyonrails.org/action_mailbox_b ...
- PPM / PGM / PBM 图像文件格式[转]
下面将详细介绍ppm文件 ppm文件是一种图像文件,有其自己的文件格式.ppm文件由两个部分组成:第一个部分是三行ASCII码,这个部分决定了图像的存储格式以及图像的特征:第二个部分就是图像的数据部分 ...
- js拖拽文件夹上传
由于项目需要上传文件到服务器,于是便在文件上传的基础上增加了拖拽上传.拖拽上传当然属于文件上传的一部分,只不过在文件上传的基础上增加了拖拽的界面,主要在于前台的交互, 从拖拽的文件中获取文件列表然后调 ...
- Ubuntu:查询计算机软硬件信息
造冰箱的大熊猫,本文适用于Ubuntu 16.04@cnblogs 2019/1/1 1.查询Linux内核版本 在命令行中通过命令“uname -rv”获取内核版本信息,执行实例如下所示.其中,标 ...
- 交换机配置——跨交换机划分VLAN配置
一.实验要求:实现跨交换地划分vlan的配置任务,使同一vlan下的主机能相互通讯 二.拓扑图如下; 三.具体实验步骤: S1交换机配置: S1>enable --进入特权模式S1#confi ...
- ACM-ICPC 2018 沈阳赛区网络预赛 I 题 Lattice's basics in digital electronics
原题链接:https://nanti.jisuanke.com/t/31450 附上队友代码:(感谢队友带飞) #include <bits/stdc++.h> using namespa ...