leetcode[105] Construct Binary Tree from Inorder and Postorder Traversal
代码实现:给定一个中序遍历和后序遍历怎么构造出这颗树!(假定树中没有重复的数字)
因为没有规定是左小右大的树,所以我们随意画一颗数,来进行判断应该是满足题意的。
3
/ \
2 4
/\ / \
1 6 5 7
中序遍历:.
后序遍历:.
我们知道后序遍历的最后一个肯定就是根了。然后在前序遍历中找到这个根,左边的就是左子树(记作subv1),右边的就是右子树(记作subv1)。在后序遍历中,前面的几个对应左子树的后序遍历(记作subv2),接下去的几个对应右子树的后序遍历(记作subv2),注意,右子树的后序遍历系系数因为3的不同位置所以要减一。
这样我们就可以分成两组的前序遍历和后序遍历的子数组了。然后递归调用返回,一个给根的左,一个给根的右。则有如下代码:
/**
* Definition for binary tree
* 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 NULL;
int rootval = postorder.back();
TreeNode *root = new TreeNode(rootval); vector<int> subv11, subv12, subv21, subv22;
bool flag = true; for (int i = ; i < inorder.size(); ++i)
{
if (inorder[i] == rootval) flag = false;
if (flag && inorder[i] != rootval)
{
subv11.push_back(inorder[i]);
subv21.push_back(postorder[i]);
}
else if (!flag && inorder[i] != rootval)
{
subv12.push_back(inorder[i]);
subv22.push_back(postorder[i - ]);
}
}
root -> left = buildTree(subv11, subv21);
root -> right = buildTree(subv12, subv22); return root;
}
};
如上,我是没次迭代的时候将数组分割成两部分,然后再对两部分分别递归求解。理论上应该是正确的。但是估计每次开辟新的vector存子数组用的空间一次一次递归用的空间太大了。以至于Memory Limit Exceeded
由上面的提示,我们知道,要达到好的效果那就不能一直开辟新的vector了,应该在原来的数组里操作,那么要达到和开辟新数组一样的效果就要用同时传入一些下标了。
中序遍历:3.
后序遍历:3.
同样的道理,首先我们要根据后序遍历的最后一个在中序遍历中分左右两部分,然后再确定后序遍历中相应部分的下标的起始位置。对每个数组我们传入两个下标参数,开始start和结束end下标。其中inorder的start记作is,end记作ie,postorder的start记作ps,end记作pe。
初始,中序遍历的开始为0,结束为inorder.size()-1.同理可以后序的也是类似。
每一次分割,中序遍历的126的开始为前一次开始is,结束为根节点3标号rootIndex-1,相应的后序遍历开始为前一次开始ps,因为长度要喝中序相同,所以结束为ps+(rootIndex-1-is)。
中序遍历的的开始为根节点3标号rootIndex+1,结束为前一次的结束ie。我们可以确定结束为pe-1,因为长度相同,所以开始为pe-1-(ie-(rootIndex+1)).
那么就有代码:
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode *fun105(vector<int> &inorder, int is, int ie, vector<int> &postorder, int ps, int pe)
{
if (is > ie || ps > pe || ie - is != pe - ps) return NULL;
TreeNode *root = new TreeNode(postorder[pe]);
int rootIndex, rootval = root -> val;
for (int i = ; i <= ie; ++i)
{
if (inorder[i] == rootval)
rootIndex = i;
} root -> left = fun105(inorder, is, rootIndex - , postorder, ps, ps + (rootIndex--is));
root -> right = fun105(inorder, rootIndex + , ie, postorder, pe - - (ie-(rootIndex+)), pe - ); return root;
}
TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder)
{
if (inorder.size() == ) return NULL;
return fun105(inorder, , inorder.size() - , postorder, , postorder.size() - );
}
};
主要的难点就是下标的控制,特别是利用长度来控制后序遍历的结束下标和起始下标。
错误的尝试:本来没有用已知长度去求后序遍历的下标,而是直接根据rootInex的下标去找后序遍历的起始,后面发现因为rootIndex在第二次迭代的时候前面已经有一个之前的根了,所以和后序遍历的下标对应就会差1,所以是错误的。迫不得已,就用已知的长度相等和已知后序遍历的某些确定下标去求未知下标了。
leetcode[105] 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 ...
随机推荐
- STL 源代码分析 算法 stl_heap.h
本文senlie原版的.转载请保留此地址:http://blog.csdn.net/zhengsenlie heap ----------------------------------------- ...
- Skynumber
Time Limit: 1000ms Memory Limit: 128000KB 64-bit integer IO format: Java class name: Submit Sta ...
- Git 常用命令手记 及 Github协同流程(转)
符号约定俗成:<xxx> 自定义内容xxx:[xxx] xxx为可选项:[<xxx>] 自定义内容xxx且为可选项. 说明/备注 命令 备注 保存更新 git add [-i] ...
- Codeforces Round #191 (Div. 2)---A. Flipping Game
Flipping Game time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- kendo ui 单击取消编辑数据grid减少的原因和治疗方法的数据
kendo ui单击取消编辑数据buttongrid数据缩减.原因grid编辑数据追打datasorce于data寻找阵列数据的存在.假定有不会加入,加入缺席. 首先一个样本: html代码: < ...
- crmplugin项目加入key文件
通常,办crm的plugin发展,然后dll文件导入系统,都需要加入项目key文件,那么怎么办? 在右上角单击指定项目--属性: 点击属性后,弹出属性编辑框: watermark/2/text/aHR ...
- 使用Mockito进行单元测试【2】—— stub 和 高级特性[转]
一篇中介绍了Mockito的基本信息,现在接着介绍Mockito强大的stub功能 2. Mockito使用实例 5. 对连续的调用进行不同的返回 (iterator-style stubbing) ...
- TableLayout中stretchColumns、shrinkColumns的使用方法
android:stretchColumns="1" android:shrinkColumns="1"这两个属性是TableLayout所特有的,也是这两个属 ...
- ABP模块系统
ABP模块系统 基于DDD的现代ASP.NET开发框架--ABP系列之4.ABP模块系统 ABP是“ASP.NET Boilerplate Project (ASP.NET样板项目)”的简称. ABP ...
- sgu 194 被动散热器具有最大流量的上限和下限(最大流量模板dinic加上优化)
模板类型的题详细參考国家集训队论文:http://wenku.baidu.com/view/0f3b691c59eef8c75fbfb35c.html 參考博客:http://blog.csdn.ne ...