(二叉树 递归) 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 <= 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.
----------------------------------------------------------------------------------------------------------------------------------
这个是从先序遍历和后序遍历构建二叉树,不过却和之前的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 ...
随机推荐
- 洛谷P3366 【模板】最小生成树(Boruvka算法)
题意 题目链接 Sol 自己yy着写了一下Boruvka算法. 算法思想很简单,就是每次贪心的用两个联通块之间最小的边去合并. 复杂度\(O(n \log n)\),然鹅没有Kruskal跑的快,但是 ...
- ubuntu server 16.04 开启root密码登录
0x00 ubuntu server 16.04 开启root密码登录 由于众多VPS默认使用证书验证登录,虽然安全但使用十分不便,所以特提供开启root用户并使用密码登录方法. 0x01 为root ...
- umijs开发实践-不同页面交叉使用dva中的modal文件导致的错误
最近在使用umijs进行H5开发工作,在开发的过程中踩了一些坑,在这里记录一下. 1:按需加载在现在是很常见的优化方式了,我在.umirc.js中开启dynamicImport后,运行umi buil ...
- 重庆3Shape Dental System技术支持
Dental System 2014中的一些新的功能:为提高生产力增添了自动冠功能软件会自动根据位置设计冠的形状,以适应周围的牙齿和拮抗剂.新的强大的用户体验优化了工作流程和一个新的重新设计的用户界面 ...
- 用Jenkins搭建自动构建服务
Jenkins是BS跨平台构建工具,之前名为Hundson.wiki [chs en] 最新windows安装包:下载 下文以1.593版本为例,讲述Jenkins的Windows版本的一些要注意 ...
- kali权限提升之配置不当提权与WCE
kali权限提升之配置不当提权与WCE 1.利用配置不当提权 2.WCE 3.其他提权 一.利用配置不当提权 与漏洞提权相比更常用的方法 在大部分企业环境下,会有相应的补丁更新策略,因此难以通过相应漏 ...
- [ gczdac ] HDU1000
地址:http://acm.hdu.edu.cn/showproblem.php?pid=1000 Problem Description Calculate A + B. Input Eac ...
- jquery中Json操作
在开发中,我们有可能拿到的不是全的json,而是一部分json格式的数据,这个时候我们需要将其强转为json对象 第一种方法:使用jquery中的$.parseJSON(),但是它对json数据格式的 ...
- Kafka监控系统Kafka Eagle:支持kerberos认证
在线文档:https://ke.smartloli.org/ 作者博客:https://www.cnblogs.com/smartloli/p/9371904.html 源码地址:https://gi ...
- 【转】手把手教你读取Android版微信和手Q的聊天记录(仅作技术研究学习)
1.引言 特别说明:本文内容仅用于即时通讯技术研究和学习之用,请勿用于非法用途.如本文内容有不妥之处,请联系JackJiang进行处理! 我司有关部门为了获取黑产群的动态,有同事潜伏在大量的黑产群 ...