关闭

Leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal

164人阅读 评论(0) 收藏 举报
分类:
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    // preorder determines the root of a tree, inorder determines the left and right child of a tree
    public static TreeNode buildTreePreIn(int[] preorder, int ps, int pe, int[] inorder, int is, int ie, HashMap<Integer, Integer> iMap) {
        
        if (ps > pe) return null;
        
        // the first element of a pre-order is the root
        TreeNode root = new TreeNode(preorder[ps]);
        if (ps == pe) return root;
        
        int index = iMap.get(preorder[ps]);
        root.left = buildTreePreIn(preorder, ps+1, index-is+ps, inorder, is, index-1, iMap);
        root.right = buildTreePreIn(preorder, index-is+ps+1, pe, inorder, index+1, ie, iMap);
        return root;
    }
    
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        HashMap<Integer, Integer> iMap = new HashMap<Integer, Integer>();
        for (int i=0; i<inorder.length; i++)
            iMap.put(inorder[i], i);
        return buildTreePreIn(preorder, 0, preorder.length-1, inorder, 0, inorder.length-1, iMap);
    }
}

0
0
查看评论

LeetCode 105:Construct Binary Tree from Preorder and Inorder Traversal

LeetCode 105: Given preorder and inorder traversal of a tree, construct the binary tree. 给定一个二叉树的前序和中序遍历,重建这棵二叉树。
  • sunao2002002
  • sunao2002002
  • 2015-05-20 23:09
  • 1045

【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51471280Subject 出处:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inor...
  • crazy1235
  • crazy1235
  • 2016-06-01 23:42
  • 5915

C++详解Leetcode:105. Construct Binary Tree from Preorder and Inorder Traversal

原题思路通过二叉树的前序遍历和中序遍历来构建二叉树,通过递归可以很容易的解决这个问题,在遇到二叉树的问题,应该习惯先画图再来解决code/** * Definition for a binary tree node. * struct TreeNode { * int val; * ...
  • u014265347
  • u014265347
  • 2017-07-30 17:34
  • 314

【LeetCode-面试算法经典-Java实现】【105-Construct Binary Tree from Preorder and Inorder Traversal(构造二叉树)】

【106-Construct Binary Tree from Preorder and Inorder Traversal(通过前序和中序遍历构造二叉树)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Given preorder and inorder traver...
  • DERRANTCM
  • DERRANTCM
  • 2015-08-09 06:37
  • 2522

[JAVA]LeetCode105 Construct Binary Tree from Preorder and Inorder Traversal

根据前序遍历和中序遍历,构建二叉树。假设树中不存在相同的节点
  • fumier
  • fumier
  • 2015-03-29 10:39
  • 740

LeetCode Construct Binary Tree from Inorder and Postorder Traversal 思考分析过程分享

思考: 1 画图: 把二叉树和其中序和后序的序列都画出来(这个很重要,我以前有时候总是凭空想象,结果脑子一片空白) 2 把问题写下来:怎么构造一个节点? 3 观察: 到底有什么特点是可以利用来解题的呢? 感觉好辛苦,一个编程任务,如临大敌似的!不知道是不是功力不到。 突然想到这是不就像是金庸武...
  • kenden23
  • kenden23
  • 2013-11-12 13:20
  • 2290

leetcode笔记:Construct Binary Tree from Preorder and Inorder Traversal

这道题考察了先序和中序遍历,先序是先访问根节点,然后访问左子树,最后访问右子树;中序遍历是先遍历左子树,然后访问根节点,最后访问右子树。
  • liyuefeilong
  • liyuefeilong
  • 2015-11-30 23:54
  • 1308

[leetcode-105]Construct Binary Tree from Preorder and Inorder Traversal(java)

问题描述: Given preorder and inorder traversal of a tree, construct the binary tree.分析:这道题实际上是考察如何从前序遍历和中序遍历中构造树。思想是:对于前序遍历而言,第一个元素一定为根节点。而对于中序遍历,根节点左边的都...
  • zdavb
  • zdavb
  • 2015-08-11 15:09
  • 276

[LeetCode][Java] 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 duplicates do not exist in the tree...
  • Evan123mg
  • Evan123mg
  • 2015-07-20 21:23
  • 649

Binary Tree Inorder Traversal -- LeetCode

原题链接: http://oj.leetcode.com/problems/binary-tree-inorder-traversal/  通常,实现二叉树的遍历有两个常用的方法:一是用递归,二是使用栈实现的迭代方法。下面分别介绍。 递归应该最常用的算法,相信大家都了解,算法的时...
  • linhuanmars
  • linhuanmars
  • 2014-03-01 05:38
  • 16805
    个人资料
    • 访问:19046次
    • 积分:1963
    • 等级:
    • 排名:千里之外
    • 原创:183篇
    • 转载:34篇
    • 译文:0篇
    • 评论:0条
    文章分类