zoukankan      html  css  js  c++  java
  • 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 duplicates do not exist in the tree.

    For example, given

    preorder = [3,9,20,15,7]
    inorder = [9,3,15,20,7]

    Return the following binary tree:

        3
       / 
      9  20
        /  
       15   7

     [暴力解法]:

    时间分析:

    空间分析:

     [优化后]:

    时间分析:

    空间分析:

    [奇葩输出条件]:

    [奇葩corner case]:

    if (preStart > preEnd || inStart > inEnd) 退出条件是>,因为等于的时候还可以继续新建节点

    [思维问题]:

    不知道怎么dc:参数就是数组的index就行了,分成start1 end1  start2 end2,多开几个变量就行了

    [英文数据结构或算法,为什么不用别的数据结构或算法]:

    [一句话思路]:

    通过hashmap记录下pre的中节点在in中的位置,然后左右dc

    [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

    [画图]:

    [一刷]:

    1. HashMap<Integer, Integer> map 函数中已经建立好了的map不需要加括号,新建map才需要

    [二刷]:

    [三刷]:

    [四刷]:

    [五刷]:

      [五分钟肉眼debug的结果]:

    [总结]:

    不知道怎么dc:参数就是数组的index就行了,分成start1 end1  start2 end2,多开几个变量就行了

    [复杂度]:Time complexity: O(n) Space complexity: O(n)

    [算法思想:迭代/递归/分治/贪心]:

    [关键模板化代码]:

    [其他解法]:

    [Follow Up]:

    [LC给出的题目变变变]:

     [代码风格] :

     [是否头一次写此类driver funcion的代码] :

     [潜台词] :

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        public TreeNode buildTree(int[] preorder, int[] inorder) {
            //corner case
            if (preorder == null && inorder == null) return null;
            
            //initialization : put all the inorder into map
            HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
            for (int i = 0; i < inorder.length; i++)
                map.put(inorder[i], i);
            
            //return 
            return buildTreeHelper(preorder, 0, preorder.length - 1, inorder, 0, inorder.length - 1, map);
        }
        
        public TreeNode buildTreeHelper(int[] preorder, int preStart, int preEnd, int[] inorder, int inStart, int inEnd, HashMap<Integer, Integer> map) {
            //exit if the bounds exceeds
            if (preStart > preEnd || inStart > inEnd) return null;
            
            //build a new root
            TreeNode root = new TreeNode(preorder[preStart]);
            int inIdx = map.get(root.val);
            int numsLeft = inIdx - inStart;
            
            //divid and conquer to form left and right
            root.left = buildTreeHelper(preorder, preStart + 1, preEnd, inorder, inStart, inIdx - 1, map);
            root.right = buildTreeHelper(preorder, preStart + numsLeft + 1, preEnd, inorder, inIdx + 1, inEnd, map);
            
            return root;
        }
    }
    View Code
  • 相关阅读:
    【转】Celery 使用入门
    OS + Ubuntu 11.6.04 qbittorrent web ui /
    java android helloworld
    web sec / linux security script / linux anquan jiagu
    java ee / java diagnosis tools cubic 1.4.1 / jvm tools cubic
    db postgres citus 10
    PapaMelon #5 设计单向链表
    First-ever Corundum Developer Meeting: the Future of Corundum
    Re: [corundum-nic] RDMA support
    赛灵思发布SN1000网络加速卡,集成NXP LX2162A
  • 原文地址:https://www.cnblogs.com/immiao0319/p/9407628.html
Copyright © 2011-2022 走看看