【树】Construct Binary Tree from Inorder and Postorder Traversal
题目:
Given inorder and postorder traversal of a tree, construct the binary tree.
思路:
后序序列的最后一个元素就是树根,然后在中序序列中找到这个元素(由于题目保证没有相同的元素,因此可以唯一找到),中序序列中这个元素的左边就是左子树的中序,右边就是右子树的中序,然后根据刚才中序序列中左右子树的元素个数可以在后序序列中找到左右子树的后序序列,然后递归的求解即可。
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {number[]} inorder
* @param {number[]} postorder
* @return {TreeNode}
*/
var buildTree = function(inorder, postorder) {
if(postorder.length==0){
return null;
}
return BuildTree(0,inorder.length-1,0,postorder.length-1,inorder,postorder);
}; function BuildTree(iStart,iEnd,pStart,pEnd,inorder,postorder){
if(pStart==pEnd){
return new TreeNode(postorder[pEnd]);
}
if(iStart>iEnd){
return null;
}
var rootval=postorder[pEnd];
var i=inorder.indexOf(rootval);
var root=new TreeNode(rootval);
root.left=BuildTree(iStart,i-1,pStart,pStart+(i-iStart)-1,inorder,postorder);
root.right=BuildTree(i+1,iEnd,pStart+(i-iStart),pEnd-1,inorder,postorder);
return root;
}
【树】Construct Binary Tree from Inorder and Postorder Traversal的更多相关文章
- 【题解二连发】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 Week14]Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告
[LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...
- 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 ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- 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 ...
随机推荐
- python 实现排列组合
1.python语言简单.方便,其内部可以快速实现排列组合算法,下面做简单介绍. 2.一个列表数据任意组合 2.1主要是利用自带的库 #_*_ coding:utf-8 _*_ #__author__ ...
- 按str 存储和按 list 存储
按str 存储1 w2 = open('./trsd_w.txt','a')#a代表追加 w代表重写 if matcher1: flag = 1 w2.write("\n") fo ...
- Airplace平台
Demo: 左上角:(0, 0)开始导航,手机终端上实时在地图上当前所在显示,当前点以绿色点显示,轨迹点以红色显示. 系统架构:基于移动手机的以网络为辅助的架构.特点:低头顶交流,用户隐私和安全 &g ...
- SharpMap源代码解析
1. 简介 SharpMap是基于.net2.0的GIS系统.支持多种.NET开发语言(C# C++ 等).使用属性数据作为注记.符合OpenGIS的简单要素规范(OpenGIS Simple Fea ...
- spring boot搭建Hello Word
一.安装与配置jdk 二.安装与配置maven 安装好maven,必须配置环境变量 通过cmd命令查询maven是否安装成功,以下是安装成功的界面 修改setting.xml的配置,制定本地仓库的路径 ...
- css控制英文内容自动换行問題
css控制英文内容自动换行 HTML: <!-- 因为要显示的内容是用户通过CMS添加进入数据库,再抓取出来前台显示的,所以你根本无法 控制每一行内容的长度. 所以我在显示内容的div设置了一个 ...
- MYC编译器源码之语法分析
MyC编译器采用自顶向下的方法进行语法解析,这种语法解析方式,一般是从最左边的Token开始,然后自顶向下看哪一条语法规则可能包含这个Token,如果包含这个Token,则自左向右根据这条语法规则逐一 ...
- Http请求基本方法
1.Http请求基本方法 /// <summary> /// Http请求基本方法 /// </summary> /// <param name="conten ...
- Android 导入 aar 库文件
1. 在需要导入 aar 的 module 目录下创建一个名叫 "aars" 的目录,并把 aar 文件复制到这里. 2. 在项目的 build.gradle 文件里添加 allp ...
- Mac OS 10.12 - ”ln: /usr/bin/tclsh: Operation not permitted“错误的解决方法!!
我在对"/usr/bin/"进行创建链接时候,出现错误:”ln: /usr/bin/tclsh: Operation not permitted“,这个错误的原因是Rootless ...