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[] and post[] are both permutations of 1, 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的更多相关文章

  1. (二叉树 递归) 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 ...

  2. (二叉树 递归) 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 ...

  3. [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 ...

  4. LeetCode 889. Construct Binary Tree from Preorder and Postorder Traversal

    原题链接在这里:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal/ 题 ...

  5. 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 ...

  6. 【LeetCode】889. Construct Binary Tree from Preorder and Postorder Traversal 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. [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 ...

  8. [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 ...

  9. [Leetcode Week14]Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...

随机推荐

  1. 《JavaScript高级程序设计》笔记:HTML5脚本编程(16)

    跨文档消息传递 跨文档消息传递(cross-document messaging),有时候简称为XDM,指的是在来自不同域的页面间传递消息.例如,www.wrox.com域中的页面与位于一个内嵌框架中 ...

  2. 亿级流量场景下,大型架构设计实现【全文检索高级搜索---ElasticSearch篇】-- 中

    1.Elasticsearch的基础分布式架构: 1.Elasticsearch对复杂分布式机制的透明隐藏特性2.Elasticsearch的垂直扩容与水平扩容3.增减或减少节点时的数据rebalan ...

  3. 长图的展开与收起(Android)

    前言: 在app的文章中,经常会夹杂着一些特别长的长图.在阅读的时候需要滑动很久才能看图片下方的文字,因此对于长图只展示图片上面一部分,并且可以展开这个功能是很重要的. 效果: 基本思路: 利用sca ...

  4. WPF:浅析Dispatcher

    本人文笔差.还是直接上代码吧.(本文假设你对WPF中的Dispatcher有一定的了解) 你觉得下面的代码可以正常执行吗? private void Button_Click(object sende ...

  5. Delphi 字符串转日期,强大到窒息,VarToDateTime 解决了 困扰很久的小问题

    procedure THRForm.Button1Click(Sender: TObject); var D:TDateTime; s:string; begin D:=VarToDateTime(' ...

  6. FFmpeg部署及相关指令操作说明

    1.首先在http://ffmpeg.zeranoe.com/builds/上下载static版本, 下载好以后解压缩到 c:/ffmpeg/ 2.配置环境变量 path -> c:/ffmpe ...

  7. .net解析csv(C#导表工具)

    前言 解析Excel有知名的NPOI库,(Java语言是POI),但是NPOI是不支持解析csv的. csv本质上也是文本文件,可以进行差异对比,更利于解决冲突. 本文对解析csv的几个.net的开源 ...

  8. 【spring源码分析】IOC容器初始化(五)

    前言:前几篇文章已经将BeanDefinition的加载过程大致分析完成,接下来继续分析其他过程. AbstractApplicationContext#refresh public void ref ...

  9. springboot 实现配置文件给常量赋值

    @Component @ConfigurationProperties(prefix = "task.cron") public class TaskCronParam imple ...

  10. ZooKeeper 之快速入门

    -----------------破镜重圆,坚持不懈! 1. 概述 Zookeeper是Hadoop的一个子项目,它是分布式系统中的协调系统,可提供的服务主要有:配置服务.名字服务.分布式同步.组服务 ...