原题链接在这里:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal/

题目:

Return any binary tree that matches the given preorder and postorder traversals.

Values in the traversals pre and post are distinct positive integers.

Example 1:

Input: pre = [1,2,4,5,3,6,7], post = [4,5,2,6,7,3,1]
Output: [1,2,3,4,5,6,7]

Note:

  • 1 <= pre.length == post.length <= 30
  • pre[] and post[] are both permutations of 1, 2, ..., pre.length.
  • It is guaranteed an answer exists. If there exists multiple answers, you can return any of them.

题解:

The first element in pre, 1 is actually the root's val.

The second element in pre, 2 is root's left child val. Find 2's index in post, before that, all are root left subtree.

Vice Versa.

Use a HashMap to store post element and its index for quick search.

In recursion, before using pre[preL+1], be careful with OutOfBoundException. Return when preL == preR. This makes sure after that, preL will not be out of index bound.

Time Complexity: O(n).

Space: O(n).

AC Java:

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode constructFromPrePost(int[] pre, int[] post) {
if(pre == null || pre.length == 0 || post == null || post.length == 0){
return null;
} HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
for(int i = 0; i<post.length; i++){
hm.put(post[i], i);
} return construct(pre, 0, pre.length-1, post, 0, post.length-1, hm);
} private TreeNode construct(int[] pre, int preL, int preR, int[] post, int postL, int postR, HashMap<Integer, Integer> hm){
if(preL > preR){
return null;
} TreeNode root = new TreeNode(pre[preL]);
if(preL == preR){
return root;
} int leftVal = pre[preL+1];
int leftIndex = hm.get(leftVal);
root.left = construct(pre, preL+1, preL+1+leftIndex-postL, post, postL, leftIndex, hm);
root.right = construct(pre, preL+2+leftIndex-postL, preR, post, leftIndex+1, postR-1, hm);
return root;
}
}

类似Construct Binary Tree from Preorder and Inorder TraversalConstruct Binary Tree from Inorder and Postorder Traversal.

LeetCode 889. Construct Binary Tree from Preorder and Postorder Traversal的更多相关文章

  1. [LeetCode] 889. Construct Binary Tree from Preorder and Postorder Traversal 由先序和后序遍历建立二叉树

    Return any binary tree that matches the given preorder and postorder traversals. Values in the trave ...

  2. (二叉树 递归) leetcode 889. Construct Binary Tree from Preorder and Postorder Traversal

    Return any binary tree that matches the given preorder and postorder traversals. Values in the trave ...

  3. LC 889. Construct Binary Tree from Preorder and Postorder Traversal

    Return any binary tree that matches the given preorder and postorder traversals. Values in the trave ...

  4. 【LeetCode】889. Construct Binary Tree from Preorder and Postorder Traversal 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  5. [LeetCode] 105. Construct Binary Tree from Preorder and Inorder Traversal 由先序和中序遍历建立二叉树

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  6. (二叉树 递归) leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  7. (二叉树 递归) leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  8. [LeetCode] 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

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

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

随机推荐

  1. Linux 网卡驱动学习(二)(网络驱动接口小结)

    [摘要]前文我们分析了一个虚拟硬件的网络驱动例子,从中我们看到了网络设备的一些接口,其实网络设备驱动和块设备驱动的功能比较类似,都是发送和接收数据包(数据请求).当然它们实际是有很多不同的. 1.引言 ...

  2. 分布式数据库数据从属与client与server的数据同步

    老实说,眼下市面上很多产品,的确是不成熟的产品. 用过一些,给人蛋痛的感觉. 导言 分布还是集总 今天我们来探讨一个非常重要的问题. 每一个程序猿都有其思想,我的思想之中的一个,就是分布式. 分布式, ...

  3. 《UNIX-Shell编程24学时教程》读书笔记Chap3,4 文件,目录操作

    Chap3 文件操作   P28 在这章中,要着重记住一些常用的选项,要有使用正则表达式的思维,能更快达到目的.----@im天行 3.1 列文件名 .profile  sh的初始化脚本: .kshr ...

  4. shell(2):正则表达式

    一.整理正则表达式博客 (1)正则 正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法.或者说:正则就是用来描述一类事物的规则. 在linux中,通配符是由shel ...

  5. 新建 .NET Core 控制台项目 C# 数组深拷贝

    新建 .NET Core 控制台项目 1. 安装 .NET Core SDK 1.0 参考微软官方网站 https://www.microsoft.com/net/download/windows 2 ...

  6. PHP几种抓取网络数据的常见方法

    //本小节的名称为 fsockopen,curl与file_get_contents,具体是探讨这三种方式进行网络数据输入输出的一些汇总.关于 fsockopen 前面已经谈了不少,下面开始转入其它. ...

  7. git clean

    使用git clean清除未加入版控的数据 作者:Level Up  发布日期:2012-12-21 10:48:10       笔者在使用版本控制软件时,不知为何常常会有些暂存的数据产生.像是下面 ...

  8. 2017-07-19-CR 和 LF

    CR 和 LF CR - Carriage Return 回车 LF - Line Feed 换行 Carriage 打字机滑轨.老式打字机,打字时,滑轨从左往右移动,一行打完了,需要把滑轨调回到最左 ...

  9. 第 2 章 第 1 题 同位词问题 下问 Multimap实现

    问题分析 输入:一个任意的单词和一个内含多个乱序单词的字典文件 输出:该单词在字典中的所有同位词 约束:允许事先对字典进行预处理 解决思路 上问的程序有个缺点 - 我们必须遍历完整个字典文件才能输出所 ...

  10. Create an OData v4 Endpoint Using ASP.NET Web API 2.2(使用ASP.NET Web API 2.2创建OData v4端点)

    开放数据协议Open Data Protocol(OData)是web的一种数据存取协议,OData通过设置CRUD操作(Create创建.Read读取.Update更新,Delete删除)提供一种统 ...