题目:

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的更多相关文章

  1. 【题解二连发】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 ...

  2. LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告

    Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...

  3. [Leetcode Week14]Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...

  4. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告

    [LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...

  5. Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...

  6. 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 ...

  7. 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 ...

  8. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...

  9. 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: ...

  10. 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 ...

随机推荐

  1. CentOS7下安装配置Nginx

    一:安装依赖项 1.pcre:2.openssl:3.zlib:4.gcc:可直接通过yum安装 二:创建nginx账户(可以配置nginx.conf的user为此账户) useradd nginx ...

  2. storyBoard中取消键盘第一响应

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {     [self.view endEditing:YES]; } ...

  3. Andfix热修复原理

    一.前言 最近腾讯弄出一个Tinker热修复框架,那么本文先不介绍这个框架,先来介绍一下阿里的一个热修复框架AndFix,这个框架出来已经很长时间了,但是看网上没有太多非常详细的讲解,这里就来做一次分 ...

  4. IT人员如何有效规划自己时间

          中午和任职 DBA 的朋友聊天,他说:"老板老是要我把工作时间填长一点,让整个部门的工作时间比较好看,所以本来做 1 个小时的事要写 3 个小时."但我回答说若这件事情 ...

  5. [Zend Mail]发送中文名附件出现乱码解决方案

    Zend Framework 1.0.* “=?UTF-8?B?”.base64_encode($title).“?=” 发送中文名附件,结果如图: 英文名附件,结果截图: 解决办法就是将中文文件名拼 ...

  6. cocos studio

    用了几天,和之前用的cocos creator以及unity的编辑器一对比,很多地方都挺反人类的哈... 拖拽和放大场景元素竟然还要切换一下编辑模式... 移动场景元素竟然没有单独控制x或者y方向的移 ...

  7. Android Sqlite 简单SQL语句

    --- 创建表 create table student(_id integer primary key autoincrement, name text); --- 查询全部 select _id, ...

  8. MySQL Route负载均衡与读写分离Docker环境使用

    Docker环境描述 主机名 部署服务 备注 MySQL Route MySQL Route 部署在宿主机上的MySQL Route服务 MySQL Master1 MySQL 5.7.16 Dock ...

  9. 唯品会osp简介(转)

    转自 https://blog.csdn.net/panyongcsd/article/details/58617810 公司(VIP)从2015年开始在内部推动Venus框架的使用,这是一款基于Ap ...

  10. python 检索一个目录下所有的txt文件,并把文件改为.log

    检索一个目录及子目录下所有的txt文件,并把txt文件后缀改为log: import os f_path = r'C:\Users\PycharmProjects\mystudy\Testfolder ...