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: ...
随机推荐
- (转)awk数组详解及企业实战案例
awk数组详解及企业实战案例 原文:http://www.cnblogs.com/hackerer/p/5365967.html#_label03.打印数组:1. [root@nfs-server t ...
- DBCP数据连接池
package com.itheima.utils; import java.io.InputStream; import java.sql.Connection; import java.sql.R ...
- C语言的各种输入情况介绍(ACM中常用到)
1.最简单的输入输出形式: 计算a+b的值: scanf("%d%d",&a,&b); printf("%d\n",a+b);--------- ...
- gof23 适配器模式
namespace Adapter { class Program { static void Main(string[] args) { //原实现 ClassBase customa = new ...
- Python介绍以及Python环境搭建
Python介绍以及Python环境搭建 1.Python 发展历史 Python是由Guido van Rossum在八十年代末和九十年代初,在荷兰国家数学和计算机科学研究所设计出来的,据说是在圣诞 ...
- oracle多表查询和子查询练习
--1.列出至少有三个员工的所有部门和部门信息. SELECT D.DEPTNO, D.DNAME, D.LOC, T.COUNTS FROM DEPT D, (SELECT DEPTNO, CO ...
- <Android 基础(九)> Ndk配置与Demo
介绍 The NDK is a toolset that allows you to implement parts of your app using native-code languages s ...
- ATL
Normal COM.cpp #include "resource.h" // 主符号 #include "ATLCOM_i.h" #include " ...
- 关于基于Linphone的视频通话Android端开发过程中遇到的问题
关于基于Linphone的视频通话Android端开发过程中遇到的问题 运用开源项目Linphone的SDK进行开发,由于是小组进行开发,我主要负责的是界面部分. 由于当时是初学Android开发,对 ...
- 《ArcGIS Runtime SDK for Android开发笔记》——(8)、关于ArcGIS Android开发的未来(“Quartz”版Beta)
1.前言 今天再一次在官网看到了ArcGIS Runtime SDK for Android下一个版本“Quartz”版的更新资料,它将是一个非常重要的更新,包括API接口的重构和开发思路的调整.具体 ...