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: ...
随机推荐
- 在rails 中使用mysql 出现Mysql::Error: Incorrect string value: 的问题
这是因为你在做数据库的操作中有非英文的问题,之后gem mysql2 处理中文必须要数据库也指定是utf-8 才比较好处理 解决的方法很简单,将数据库每张表都转化成utf-8即可,如果数据库没有什么重 ...
- stm32 外部中断学习
今天我们看看STM32的外部中断实验. STM32 供 IO 口使用的中断线只有 16 个,但是 STM32 的 IO 口却远远不止 16 个,那么 STM32 是怎么把 16 个中断线和 IO 口一 ...
- Android自定义进度条-带文本(文字进度)的水平进度条(ProgressBar)
水平进度条,显示进度的文本随着进度而移动. 效果如下,截的静态图. 代码如下 TextProgressBar.java public class TextProgressBar extends Pro ...
- Hibernate课程 初探多对多映射2-1 创建数据库表
--创建表 -- 创建项目表 create table project( proid int(10) not null comment '项目Id', proname varchar(30) co ...
- Http和Https的区别--笔记
学习链接: 知乎:https://www.zhihu.com/question/19577317 法号桑菜 http://blog.csdn.net/jasonjwl/article/details/ ...
- js的垃圾收集机制以及写代码如何处理
程序都自己的内存,一旦内存过多就会清楚以前的缓存.所以,在写代码的时候,不要仅仅只会推变量到栈中,还要会将变量从栈中释放. 那么问题来了,我们应该如何将内存从栈中释放呢? 要释放变量,那就要从java ...
- 文件上传PHP
<?php $targetIp = GetIP(); $fileUpload = 'fileUpload'; $frameCount = 'frameCount'; $fileName = $_ ...
- 视频会议20方100点 v2.66.1.18
平台: Windows 类型: 虚拟机镜像 软件包: 视频会议服务器( Video Conference Server ) 20-party video conference business int ...
- shell去重
sort命令可以对文本的内容进行排序 uniq命令可以对文本内容连续的内容进行去重,非连续的重复内容无法去重 sort 文件 | uniq 可以达到去除所有重复数据的目的(因为先排序了,这样相同的内容 ...
- 拼接sql语句时拼接空字符串报sql错误
先上代码(php): $id_card=""; $sql = "select * from people where id_card=".$id_card; 看 ...