LeetCode106. Construct Binary Tree from Inorder and Postorder Traversal
题目
根据一棵树的中序遍历与后序遍历构造二叉树。
注意:
你可以假设树中没有重复的元素。例如,给出
中序遍历 inorder = [9,3,15,20,7]
后序遍历 postorder = [9,15,7,20,3]返回如下的二叉树:
3
/ \
9 20
/ \
15 7
Tag
dfs . post+inorder .递归。二分查找
代码
还是跟105一样的思路。注意mid此时是post的最后一个。并且递归时,先递归右子树,再递归左子树。因为是后序遍历。
//recursive .dfs
class Solution {
public:
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
int mid= postorder.size()-1 ;//后序遍历。根节点在最后一个。
return Helper(inorder,postorder,mid,0,postorder.size()-1);
}
TreeNode* Helper(vector<int>& inorder,vector<int>& postorder,int & mid ,int start ,int end )
{
if(start>end||mid<0)
return nullptr;
TreeNode* root=new TreeNode(postorder[mid]);
auto pos = distance(inorder.begin(),find(inorder.begin()+start,inorder.begin()+end,postorder[mid]) );
mid--;//从后面找根节点
//后序是 先递归右子树。再递归左子树
root->right=Helper(inorder,postorder,mid,pos+1,end);
root->left=Helper(inorder,postorder,mid,start,pos-1);
return root;
}
};
问题
LeetCode106. Construct Binary Tree from Inorder and Postorder Traversal的更多相关文章
- Leetcode106. Construct Binary Tree from Inorder and Postorder Traversal中序后续构造二叉树
根据一棵树的中序遍历与后序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 中序遍历 inorder = [9,3,15,20,7] 后序遍历 postorder = [9,15, ...
- Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- 36. Construct Binary Tree from Inorder and Postorder Traversal && Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal OJ: https://oj.leetcode.com/problems/cons ...
- LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal
LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...
- 【题解二连发】Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree from Preorder and Inorder Traversal
LeetCode 原题链接 Construct Binary Tree from Inorder and Postorder Traversal - LeetCode Construct Binary ...
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- [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: ...
随机推荐
- 性能测试工具LoadRunner31-LR之链接mysql
步骤: 1.建好mysql数据库并启动 2.下载libmysql.dll,放到保存脚本的文件夹下 3.编写脚本并运行 Action() { int rc; //定义状态变量,0表示成功,非0表示失败 ...
- IntelliJ IDEA里找不到javax.servlet的jar包
此处有小坑,请注意: https://mvnrepository.com网站查询到的servlet的包的格式为: provided group: 'javax.servlet', name: 'jav ...
- 注册中心eureka
最近在忙一些其它的事情,两个城市来回跑还要办一些手续,挺费劲的,学习的事情也就耽误了一些,尽量赶吧. spring cloud为分布式的微服务架构提供了一站式的解决方案,eureka注册中心在spri ...
- Docker学习笔记(1)-简介
1. 简介 Docker使用Google公司推出的Go语言开发实现,基于Linux内核的cgroup, namespace以及AUFS类的UnionFS等技术,对进程进行封装隔离,属于操作系统层面的虚 ...
- HDU 5353—— Average——————【贪心+枚举】
Average Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total S ...
- Python常用模块一
一. os模块 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目录:相当于shell下cd ...
- SpringBoot如何集成Jedis
添加jedis依赖 在项目pom.xml文件中添加依赖 <!-- 添加jedis依赖 --> <dependency> <groupId>redis.clients ...
- struts2分页实现
1.定义一个page类,里面包括每一个的数量,当前页码,总记录数,等 2.将page对象传入dao中,dao输出分页查询好的数据.返回到action中,action放到request中,jsp从req ...
- Gremlin--一种支持对图表操作的语言
Gremlin 是操作图表的一个非常有用的图灵完备的编程语言.它是一种Java DSL语言,对图表进行查询.分析和操作时使用了大量的XPath. Gremlin可用于创建多关系图表.因为图表.顶点和边 ...
- XCode 如何真机运行别人的demo项目
iOS应用安装到真机需要证书和mobileprovision 文件,拿到别人的项目 是没有这些的 ,也就运行不起来. 要想运行起来, 需要选中项目, target - > 修改 bundlei ...