原题链接在这里: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. Android常见的三种内部类

    在java里类中再定义类,这种在其他类内部类叫做内部类,在Android开发里最常见有三种内部类分别是(成员内部类.方法内部类.匿名内部类) 一.成员内部类 1 public class Test { ...

  2. Direct2D教程(一)Direct2D已经来了,谁是GDI的终结者?

    什么是Direct2D 一言以蔽之,就是Windows 7平台上的一个2D图形API,可以提供高性能,高质量的2D渲染.大多数人对Direct2D可能都比较陌生,以至于我之前在论坛上提到这个词的时候, ...

  3. NFC模组,开发NFC功能 仅仅要几条指令的事情

    特点:实现NFC透明传输.内置NFC协议栈,支持UART串口直接读写,用于门禁能够同一时候兼容手机和卡片开门,还能实现动态密钥,读到的NFC数据自己主动串口输出,会串口就能开发NFC,不须要研究LLC ...

  4. 转:在CentOS下编译安装GCC

    转:https://teddysun.com/432.html 在CentOS下编译安装GCC 技术  秋水逸冰  发布于: 2015-09-02  更新于: 2015-09-02  6519 次围观 ...

  5. linux 设置ssh密钥登录

    一.查看服务器上是否开启了sshd服务 要确认linux是不是安装了和开启了ssh服务可以输入 netstat -an|grep 22,查看22端口是否打开,如果找开,则证明SSH服务开启了. ps ...

  6. Web框架Django(一)

    Python的WEB框架有Django.Tornado.Flask 等多种,Django相较与其他WEB框架其优势为:大而全,框架本身集成了ORM.模型绑定.模板引擎.缓存.Session等诸多功能. ...

  7. 关于ASP.NET MVC中Response.Redirect和RedirectToAction的BUG (跳转后继续执行后面代码而不结束进程)以及处理方法

    关于ASP.NET MVC中Response.Redirect和RedirectToAction的BUG (跳转后继续执行后面代码而不结束进程)以及处理方法   在传统的ASP.NET中,使用Resp ...

  8. 英特尔和Red Hat合作实现Gnome桌面的Wayland支持

    在发布支持XMir的Linux图形驱动程序xf86-video-intel 2.99.901后数天,英特尔宣布撤回对XMir的支持,XMir补丁不会合并到上游项目.XMir是Mir显示服务器的X11兼 ...

  9. kubernetes对象之Ingress

    系列目录 概述 向外网暴露集群内服务,以使客户端能够访问,有以下几种方法,本文重点描述Ingress. LoadBalancer LoadBalancer一般由云服务供应商提供或者用户自定义,运行在集 ...

  10. IOS 学习记录

    表情排列 // // MJViewController.m // 01-表情排列 // // Created by apple on 13-11-24. // Copyright (c) 2013年 ...