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 <= 30pre[]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 ...
随机推荐
- Delphi MSCom安装
- shell脚本基础和grep文本处理工具企业应用1
bash特性及bash脚本编程初步: 用户要跟计算机交互就需要有终端,比如:显示器.键鼠等,在终端有附着的接口程序 GUI:KDE.GNome.Xfce CLI:/etc/shells bash ...
- 解决docker容器的窗口大小问题
解决docker容器的窗口大小问题 最近哥们在是使用docker时,发现有些容器内部窗口大小有问题. 如下午所示,vi窗口只占据左上角一部分.正常情况下vi应该铺满整个窗口才对呀. 所以哥们找到了解决 ...
- dockerfile 与 docker-compose的区别
https://blog.csdn.net/londa/article/details/91815208 先简单理解 docker 的使用过程,它分为镜像构建与容器启动. 镜像构建:即创建一个镜像,它 ...
- TCP协议(包括TCP的连接过程,数据分段,TCP有关服务器优化)
Transmission Control Protocol/Internet Protocol 传输控制协议/因特网互联协议 TCP/IP是一个Protocol Stack(协议栈),包括TCP.IP ...
- Html中使用Cookie取值赋值
//设置Cookie function setCookie(name, value) { var Days = 1; var exp = new Date(); exp.setTime(exp.get ...
- 自签名ssl
openssl req -nodes -newkey rsa:1024 -out myreq.pem -keyout privatekey.pem openssl req -in myreq.pem ...
- (转)rotatelogs - Piped logging program to rotate Apache logs
原文:http://publib.boulder.ibm.com/httpserv/manual60/programs/rotatelogs.html rotatelogs is a simple p ...
- MVVM(使用Prism框架)开发WPF
[MVVM]目的是为了分离视图(View)和模型(Model)的耦合——解耦 1.View负责前端展示,与ViewModel进行数据和命令的交互.(双向的数据属性传递,单向的命令属性传递View→Vi ...
- Python 特点
优点 简单 -- Python 是一种代表简单主义思想的语言.阅读一个良好的 Python 程序就感觉像是在读英语一样,尽管这个英语的要求非常严格!Python 的这种伪代码本质是它最大的优点之一.它 ...