leetcode-1006 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 duplicates do not exist in the tree.
题意:根据中序遍历和后序遍历,构建二叉树
思路很清晰,做法很简单,就不讲了。
一开始我写了一个递归的解法,本地测试数据都OK,无奈提交的时候内存超出限制,下面先给出超出内存的代码:
/**
* 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* buildTree(vector<int>& inorder, vector<int>& postorder) {
if(inorder.size()==)
return nullptr;
if(inorder.size()==)
return new TreeNode(inorder[]);
int fath=postorder[postorder.size()-];
TreeNode* root=new TreeNode(fath);
int flag=-;
for(int i=;i<inorder.size();i++)
{
if(inorder[i]==fath)
{
flag=i;
break;
}
}
vector<int> left;
for(int i=;i<flag;i++)
left.push_back(inorder[i]);
vector<int> right;
for(int i=flag+;i<inorder.size();i++)
right.push_back(inorder[i]);
int flag1=-;
for(int i=;i<postorder.size();i++)
{
if(postorder[i]==inorder[flag])
{
flag1=i;
break;
}
} vector<int> left1;
for(int i=;i<flag1;i++)
left1.push_back(postorder[i]);
vector<int> right1;
for(int i=flag1;i<postorder.size()-;i++)
right1.push_back(postorder[i]); root->left=buildTree(left,left1);
root->right=buildTree(right,right1);
return root;
}
};
有没有看出问题,没错,就是第28、31、44、47行的代码,每次递归都会产生新的vector数组,所以最后导致内存超出限制。所以改进了一下,新定义一个方法helper来递归,helper里面的实现不再申请新的vector空间,直接在参数inorder和postorder中进行操作,从而避免内存超出限制。下面是accepted的代码:
/**
* 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* buildTree(vector<int>& inorder, vector<int>& postorder) {
return helper(inorder,,inorder.size()-,postorder,,postorder.size()-);
} TreeNode* helper(vector<int>& inorder,int begin1,int end1,vector<int>& postorder,int begin2,int end2)
{
if(begin1>end1)
return nullptr;
if(begin1==end1)
return new TreeNode(inorder[begin1]); TreeNode* root=new TreeNode(postorder[end2]);
int i=begin1;
for(;i<=end1;i++)
{
if(inorder[i]==postorder[end2])
break;
}
int leftlen=i-begin1; root->left=helper(inorder,begin1,begin1+leftlen-,postorder,begin2,begin2+leftlen-);
root->right=helper(inorder,begin1+leftlen+,end1,postorder,begin2+leftlen,end2-);
return root;
}
};
leetcode-1006 Construct Binary Tree from Inorder and Postorder Traversal的更多相关文章
- [Leetcode Week14]Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...
- Java for LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 31041 Total Submissions: ...
- leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree f
1. Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder travers ...
- (二叉树 递归) 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] 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 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 ...
- C#解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 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树 C++
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- 【leetcode】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[105] Construct Binary Tree from Inorder and Postorder Traversal
代码实现:给定一个中序遍历和后序遍历怎么构造出这颗树!(假定树中没有重复的数字) 因为没有规定是左小右大的树,所以我们随意画一颗数,来进行判断应该是满足题意的. 3 / \ 2 4 /\ / \1 6 ...
随机推荐
- R语言实战(四)回归
本文对应<R语言实战>第8章:回归 回归是一个广义的概念,通指那些用一个或多个预测变量(也称自变量或解释变量)来预测响应变量(也称因变量.效标变量或结果变量)的方法.通常,回归分析可以用来 ...
- 转化秒数为正规的时间格式{NSString格式的秒数转成NSDate格式后再以NSString形式输出)
-(NSString*)changeNumToTime:(NSString*)str { NSDate *date = [NSDate dateWithTimeIntervalSince1970:[s ...
- STM32-USB详细使用说明(转)
源:STM32-USB详细使用说明 附件HID的双向通信 亮点STM32开发板充实了USBHID数据发送和接收例程(STM32固件库3.5 USB库3.4)
- leetcode--001 max point on a line
package leetcode; import java.util.HashMap; class Point{ int x; int y; Point(){ x=0; y=0; } Point(in ...
- Nodejs之使用session
nodejs中使用session的说明. session介绍 为什么使用session: session运行在服务器端,当客户端第一次访问服务器时,可以将客户的登陆信息保存. 当客户访问其他界面时,可 ...
- 测试MarsEdit
测试MarsEdit 今天在MAC上使用MarsEdit编写第一篇博客,测试使用. 今天在MAC上使用MarsEdit编写第一篇博客,测试使用. -(void)myBtnAction:(UIButto ...
- C#常用网址
C# 编程指南 https://msdn.microsoft.com/zh-cn/library/67ef8sbd.aspx
- css3 翻牌效果
<!DOCTYPE html> <head> <meta http-equiv="Content-Type" content="text/h ...
- iOS 之 界面编程解析
参考:http://www.cocoachina.com/design/20151225/14789.html 0. 内容概述 基础与本质:说明普遍意义上的UI系统的三大模块,让读者从整体上对UI系统 ...
- JavaScript定时机制、以及浏览器渲染机制 浅谈
昨晚,朋友拿了一道题问我: a.onclick = function(){ setTimeout(function() { //do something ... },0); }; JavaScript ...