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 ...
随机推荐
- tableview的reloadData应注意
http://blog.csdn.net/ouyangtianhan/article/details/7835041 http://stackoverflow.com/questions/160715 ...
- 在线文档转换API word,excel,ppt等在线文件转pdf、png
在线文档转换API提供word,excel,ppt等在线文件转pdf.png等,文档:https://www.juhe.cn/docs/api/id/259 接口地址:http://v.juhe.cn ...
- Android线程之基本用法
一: 在android中有两种实现线程thread的方法: 一种是,扩展java.lang.Thread类 另一种是,实现Runnable接口 二: Thread类代表线程类,它的两个最主要的方法是: ...
- iOS开发——浅谈构架与用户体验
工作不是千篇一律的重复,从中寻找乐趣才是我们应该做的. 作为一名码农,做过几个项目,每次做项目的时候都会自己构思,如果完全是我自己设计,会怎么去设计?心里一直没有满意的答案,不管怎么布局,好像都感觉差 ...
- ios 计算字符串长度<转>
- (int)textLength:(NSString *)text//计算字符串长度 { float number = 0.0; for (int index = 0; index ...
- 【转】国外程序员整理的 C++ 资源大全
内容包括:标准库.Web应用框架.人工智能.数据库.图片处理.机器学习.日志.代码分析等. 标准库 C++标准库,包括了STL容器,算法和函数等. C++ Standard Library:是一系列类 ...
- 集群下Cookie共享,必须要设置machineKey
这个节允许你设置用于加密数据和创建数字签名的服务器特定的密钥.ASP.NET自动使用它来保护表单验证Cookie,你也可以将它用于受保护的视图状态数据.同时,这个密钥还用于验证进程外的会话状态提供程序 ...
- [Angular Tutorial] 8 - Templating Links & Images
在这一步中,我们将会在电话列表中为电话添加略图,并附上链接,当然现在也不会链接去哪.在随后的步骤中,我们将使用这些链接来展示电话列表中额外的信息. ·现在电话列表中会有链接和图片. 最重要的不同在下面 ...
- java--反射和注解
一.java.lang.reflect类 Class类 1.反射机制(Reflection):通过类创建对象, 2.反射机制提供了如下功能: 在运行时,判断任意一个对象所属的类 构造任意一个类的对 ...
- Python使用Selenium/PhantomJS
安装selenium: 1 pip install selenium 安装PhantomJS: 1 2 3 4 https://bitbucket.org/ariya/phantomjs/downlo ...