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 ...
随机推荐
- Java学习笔记【一、环境搭建】
今天把java的学习重新拾起来,一方面是因为公司的项目需要用到大数据方面的东西,需要用java做语言 另一方面是原先使用的C#公司也在慢慢替换为java,为了以后路宽一些吧,技多不压身 此次的学习目标 ...
- 【3】Zookeeper中的角色
一.Zookeeper角色的分类 领导者(leader) Leader服务器为客户端提供读服务和写服务. 学习者(learner) 跟随者(follower) Follower服务器为客户端提供读服务 ...
- Linux中/etc/inittab文件
1. inittab基本概念 a) init进程: Linux在完成核内引导(内核镜像已被载入内存,开始运行,并已初始化所有的设备驱动程序和数据结构等)之后,接着通过启动一个用户级程序init来启动其 ...
- 解决xshell连接不上阿里云服务器问题
最近购买了阿里云服务器准备玩玩,但是使用xshell连接阿里云服务器时,系统一直提示“Connection established. To escape to local shell, press ' ...
- dsoframer控件注册,解注册和检查注册情况
public class DsoframerHelper { private static string dsoframerPath = System.Windows.Forms.Applicat ...
- mybatis配置加载源码概述
Mybatis框架里,有两种配置文件,一个是全局配置文件config.xml,另一个是对应每个表的mapper.xml配置文件.Mybatis框架启动时,先加载config.xml, 在加载每个map ...
- mybatis 注解@Results、@Result、@ResultMap、@One的使用
- python脚本攻略之log日志
1 logging模块简介 logging模块是Python内置的标准模块,主要用于输出运行日志,可以设置输出日志的等级.日志保存路径.日志文件回滚等:相比print,具备如下优点: 可以通过设置不同 ...
- Hadoop-No.13之数据源系统以及数据结构
文件系统中采集数据时,应该考虑以下内容. 数据源系统设备的读取速率 在所有处理流水线中,磁盘I/O通常都是主要瓶颈.但是优化采集流程时通常要看一下检索数据的系统系统.一般来说,Hadoop的读取速度在 ...
- JAVA访问Zabbix API
Zabbix 一.Zabbix 概述 zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案.zabbix能监视各种网络参数,保证服务器系统的安全运营:并提供灵活的 ...