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 ...
随机推荐
- DDD领域驱动设计仓储Repository
DDD领域驱动设计初探(二):仓储Repository(上) 前言:上篇介绍了DDD设计Demo里面的聚合划分以及实体和聚合根的设计,这章继续来说说DDD里面最具争议的话题之一的仓储Repositor ...
- 在ABP项目的应用Castle Windsor
Castle Windsor常用介绍以及其在ABP项目的应用介绍 最近在研究ABP项目,有关ABP的介绍请看阳光铭睿 博客,ABP的DI和AOP框架用的是Castle Windsor下面就对Castl ...
- android在当前app该文件下创建一个文件夹
/********************************************************************* * Author : Samson * Date ...
- Java并发编程:线程池的使用(转)
Java并发编程:线程池的使用 在前面的文章中,我们使用线程的时候就去创建一个线程,这样实现起来非常简便,但是就会有一个问题: 如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了, ...
- 【IOS 开发】Object-C 入门 Xcode 环境具体解释
作者 : 韩曙亮 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/38424965 一. Xcode 环境安装 与 project ...
- mySQl该数据库不能将中国的做法
1. 更改MySQL安装文件夹(C:\Program Files\MySQL\MySQL Server 5.5)的my.ini文件 设置: default-character-set=utf8 cha ...
- Unity3D脚本--真实1
1. Unity3D动作脚本 Unity3D脚本用于Unity3D发动机订单公布. JavaScript全局变量:在Inspector中能够看到,且能够改动其值.其他脚本可调用此变量. C#公有(pu ...
- 你可以不知道原因,但是,我们不能停止努力。httplook抓取路由配置界面信息
因为实验室的项目要求,需要动态控制路由的配置信息,我们知道.路由选择需要购买的访问后,:http://192.168.1.1 路由配置页面.配置,不须要人手工操作.甚至定时任务配置的时候,就须要进一步 ...
- js 实现键盘记录 兼容FireFox和IE
这两天突然想弄弄js的键盘记录,所以就小研究了一下. 主要分四个部分 第一部分:浏览器的按键事件 第二部分:兼容浏览器 第三部分:代码实现和优化 第四部分:总结 第一部分:浏览器的按键事件 用js实现 ...
- oracle_删除同一列的重复数据
<!--删除同一列的重复数据 rowid 在orcle中 数据的物理地址---> delete from tbl_over_picture_alarm a where rowid not ...