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. <libxml2/tree.h> file not found

    Build Settings: head search paths :$(inherited) /usr/include/libxml2 Build phases: 加上libxml2.tbd

  2. BZOJ2064: 分裂

    2064: 分裂 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 360  Solved: 220[Submit][Status][Discuss] De ...

  3. linux 套接字编程入门--Hello World

    下述代码是linux套接字编程的入门代码.分为服务端和客户端源码. 服务端代码的主要流程是绑定ip地址和端口号建立套接字,等待客户端发起访问.接受客户端请求之后,向客户端发送字符串"hell ...

  4. ksoap2- webservice

    1.概述 对于J2ME访问远端的Web Service,除了官方标准JSR 172,我们还有两种选择: l         kSOAP l         Wingfoot Wingfoot是由Win ...

  5. 小结IE6的坑

    1.z-index在position:relative/absolute等定位属性设置后还是无效,会导致top栏的导航栏目的子菜单被下面的层遮住,无法显示:解决办法:?http://www.wufan ...

  6. WinAPI: GetClassName - 获取指定窗口的类名

    WinAPI: GetClassName - 获取指定窗口的类名 //声明: GetClassName( hWnd: HWND; {指定窗口句柄} lpClassName: PChar; {缓冲区} ...

  7. @dynamic、@synthesize

    声明property属性后,有2种实现选择: @synthesize 编译器期间,让编译器自动生成getter/setter方法. 当有自定义的存或取方法时,自定义会屏蔽自动生成该方法 @dynami ...

  8. shell 命令合并文本

    之前想把代码打印出来看来着,后来合并完之后放在word里发现有2000多页,然后放弃了~anyway,这个命令还是挺有用的. 比如我有文本a001.dat, a002.dat, a003.dat .. ...

  9. Spring aop:decare-parent 为类增加新的方法

    Spring aop:decare-parent 为类增加新的方法: 使用XML配置的方式: XML: <?xml version="1.0" encoding=" ...

  10. MalformedObjectNameException: Invalid character '' in value part of property

    http://blog.csdn.net/getdate/article/details/6729706 ojdbc6.jar的问题: 最近在项目中用spring配置oracle数据库连接池, 启动的 ...