(二叉树 递归) 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 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.
----------------------------------------------------------------------------------------------------------------------------------
这个是从先序遍历和后序遍历构建二叉树,不过却和之前的leetcode105 和 leetcode106的从中序遍历和先序遍历构建二叉树以及中序遍历和后序遍历构建二叉树不同的是,这个构建二叉树是不唯一的。另外,实现的代码和之前的两个题是也有一些不同。(注:关于这个怎么确定从先序遍历和后序遍历构建二叉树,可以看官方题解:https://leetcode.com/articles/construct-binary-tree-from-preorder-and-postorder-/ 和另一个大佬的博客https://blog.csdn.net/waple_0820/article/details/81837875)
C++代码1:
这个代码和leetcode 105 以及106的的用时20多ms的代码相比,添加了其中一个数组的长度(pre.size()),这样的会方便确定递归遍历时的终止点。不过有些代码是多余的。
/**
* 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:
TreeNode* constructFromPrePost(vector<int>& pre, vector<int>& post) {
return build(pre,,pre.size()-,post,,pre.size() - ,pre.size());
}
TreeNode* build(vector<int> &pre,int prel,int prer,vector<int> &post,int posl,int posr,int N){
if(prel > prer || posl > posr) return NULL;
if(N == )
return NULL;
TreeNode *cur = new TreeNode(pre[prel]);
if(N == )
return cur;
int i = ;
for(i = ; i < N; i++){
if(pre[prel + ] == post[posl + i - ])
break;
}
cur->left = build(pre,prel + ,prel + i,post,posl,posl + i - ,i);
cur->right = build(pre,prel + i + ,prer,post,posl + i,posr-,N--i);
return cur;
}
};
C++代码2:
去除以上的多余的代码:build()里面的参数去掉了pre和post数组的末端的数的下标,不过保留了pre和post数组的长度,这个也会确定pre的末端的数的下标。
/**
* 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:
TreeNode* constructFromPrePost(vector<int>& pre, vector<int>& post) {
return build(pre,,post,,pre.size());
}
TreeNode* build(vector<int> &pre,int prel,vector<int> &post,int postl,int N){
if(N == ) return ;
TreeNode *cur = new TreeNode(pre[prel]);
if(N == ) return cur;
int i = ;
for(i = ; i < N; i++){
if(pre[prel + ] == post[postl + i - ])
break;
}
cur->left = build(pre,prel + ,post,postl,i);
cur->right = build(pre,prel + i + ,post,postl + i,N - - i);
return cur;
}
};
C++代码3:
和leetcode105和106的用时150ms以上的代码基本类似,就是 if(pre.size() == 1 || post.size() == 1) return cur;这个代码不能漏掉,这个是必须写上的递归的终止条件。还有,这个代码耗时比较少。。。。才20多ms。
/**
* 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:
TreeNode* constructFromPrePost(vector<int>& pre, vector<int>& post) {
return build(pre,post);
}
TreeNode* build(vector<int> &pre,vector<int> &post){
if(pre.size() == || post.size() == ) return NULL;
int rootval = pre[];
TreeNode *cur = new TreeNode(rootval);
if(pre.size() == || post.size() == ) return cur;
int i = ;
for(i = ; i < pre.size(); i++){
if(pre[] == post[i - ])
break;
}
vector<int> prel,prer,postl,postr;
for(int k = ; k <= i; k++){
prel.push_back(pre[k]);
}
for(int k = i + ; k < pre.size(); k++){
prer.push_back(pre[k]);
}
for(int k = ; k < i; k++){
postl.push_back(post[k]);
}
for(int k = i; k < post.size() - ; k++){
postr.push_back(post[k]);
}
cur->left = build(prel,postl);
cur->right = build(prer,postr);
return cur;
}
};
(二叉树 递归) leetcode 889. Construct Binary Tree from Preorder and Postorder Traversal的更多相关文章
- (二叉树 递归) 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 ...
- (二叉树 递归) 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 ...
- [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/ 题 ...
- 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 trave ...
- 【LeetCode】889. Construct Binary Tree from Preorder and Postorder Traversal 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [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 ...
- [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 ...
- [Leetcode Week14]Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...
随机推荐
- 超级有爱的五款APP共享 可以让你神操作
随着科技的不断发展,手机功能的不断完善,让我们更加依赖手机,不得不说手机给我们带来很多的乐趣和方便. 今天就主要给大家分享五款超级有爱的APP软件,感兴趣的小伙伴已经迫不及待了吧! 荔枝 荔枝是一款声 ...
- ocelot 自定义认证和授权
ocelot 自定义认证和授权 Intro 最近又重新启动了网关项目,服务越来越多,每个服务都有一个地址,这无论是对于前端还是后端开发调试都是比较麻烦的,前端需要定义很多 baseUrl,而后端需要没 ...
- Oracle dblink的连接模式的关系测试总结
这篇主要介绍一下database link由于连接数据库的方式不同遇到的一些问题,我们知道连接ORACLE服务器的模式一般有两种方式:专用服务器连接(dedicated server)和共享服务器连接 ...
- VS2017内存占用高
我的环境和硬件参数 说明:本篇所提到的方法在我的机器上经过设置是能明显改善卡顿的,但可能你的VS卡顿的原因不一定是本文所提到的,可以通过排除法找到问题所在. 我的环境和硬件参数: vs 2017 pr ...
- 【学习记录】第一章 数据库设计-《SQL Server数据库设计和开发基础篇视频课程》
一.课程笔记 1.1 软件开发周期 (1)需求分析阶段 分析客户的业务和数据处理需求. (2)概要设计阶段 设计数据库的E-R模型图,确认需求信息的正确和完整. /* E-R图:实体-关系图(Ent ...
- windows10下安装kali子系统
写在前面 为什么我会想到在窗下装一个卡利 作为一个小白,平时做CTF题的时候,有时会用到python2.7环境(比如一些脚本需要,还有窗户下用的SqlMap的话,好像只支持在python2.7,之前被 ...
- 【心得】Lattice后端使用经验小结(ECP5UM,DDR3,Diamond3.10,Reveal逻辑分析)
[博客导航] [导航]FPGA相关 背景 下边的内容,适合初次使用Lattice的.具备FPGA开发经验的同学. 1.初次使用,还真遇到不少的坑,Lattice的工具也有不少优缺点,通过总结,希望能缩 ...
- Python爬虫【实战篇】scrapy 框架爬取某招聘网存入mongodb
创建项目 scrapy startproject zhaoping 创建爬虫 cd zhaoping scrapy genspider hr zhaopingwang.com 目录结构 items.p ...
- leetcode 678. Valid Parenthesis String
678. Valid Parenthesis String Medium Given a string containing only three types of characters: '(', ...
- iOS 打包.framework(包括第三方、图片、xib、plist文件)详细步骤及需要注意的地方
https://www.cnblogs.com/yk123/p/9340268.html // 加载自定义名称为Resources.bundle中对应images文件夹中的图片// 思路:从mainb ...