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的更多相关文章

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

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

  2. 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: ...

  3. leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal &amp; Construct Binary Tree f

    1.  Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder travers ...

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

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

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

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

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

  9. 【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 ...

  10. leetcode[105] Construct Binary Tree from Inorder and Postorder Traversal

    代码实现:给定一个中序遍历和后序遍历怎么构造出这颗树!(假定树中没有重复的数字) 因为没有规定是左小右大的树,所以我们随意画一颗数,来进行判断应该是满足题意的. 3 / \ 2 4 /\ / \1 6 ...

随机推荐

  1. Spring AOP进行日志记录,管理

    在java开发中日志的管理有很多种.我一般会使用过滤器,或者是Spring的拦截器进行日志的处理.如果是用过滤器比较简单,只要对所有的.do提交进行拦截,然后获取action的提交路径就可以获取对每个 ...

  2. NavigationControllerr滑动返回

    iOS 7中在传统的左上角返回键之外,提供了右滑返回上一级界面的手势.支持此手势的是UINavigationController中新增的属性 interactivePopGestureRecogniz ...

  3. jQuery源码学习(2):选择器初窥

    选择器初窥 代码架构: jQuery选择器可以依照传入数据的类型分为五大类: 传入字符串:$("div"), $("#id"), $(".div1&q ...

  4. IOS开发中使用AFNetworking请求网络数据

    1.把AFNetworking的文件拖放到项目中(注意不同的版本方法不一样,本历程基于版本2013): 2.使用#import "AFNetworking.h"命令把AFNetwo ...

  5. UVa 727 - Equation

    题目大意:给一个中缀表达式,转换成后缀表达式. 这类题一直不太会,让我想就是建一棵表达式树,然后后续遍历算了,可是建树的过程实在太麻烦了.今天才看到有中缀表达式转换成后缀表达式的算法,可以用栈进行实现 ...

  6. CodeForces 755C PolandBall and Forest (并查集)

    题意:给定每一点离他最远的点,问是这个森林里有多少棵树. 析:并查集,最后统计不同根结点的数目即可. 代码如下: #pragma comment(linker, "/STACK:102400 ...

  7. mysql存储过程中in、out、inout参数使用实际案例

    1.参数in的使用(代表输入,意思说你的参数要传到存过过程的过程里面去)//为了避免存储过程中分号(";")结束语句,我们使用分隔符告诉mysql解释器,该段命令是否已经结束了./ ...

  8. SNPs & MAF

    SNPs,全称是single nucleotide polymorphisms,SNPs等位基因频率的容易估计.采用混和样本估算等位基因的频率是种高效快速的策略.该策略的原理是:首先选择参考样本制作标 ...

  9. 关于网页显示乱码问题的一些个人见解(PHP、JSP...)

    最近做项目,遇到了一些网页显示乱码的情况,在网上查了很多资料都没有给一个全面的准确的答案,自己摸索了一下经过对比开发环境(我使用的是Myeclipse)编辑器的编码和浏览器默认显示的编码发现,在字符编 ...

  10. [Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds...

    INSERT INTO `ftms_active_dealer`(dealer_code,dealer_name,active_id,dealer_state)VALUES('415A1','贺磊'1 ...