zoukankan      html  css  js  c++  java
  • [LeetCode#106]Construct Binary Tree from Inorder and Postorder Traversal

    The problem:

    Given inorder and postorder traversal of a tree, construct the binary tree.

    Note:
    You may assume that duplicates do not exist in the tree.

    My analysis:

    This kind of question is elegant and meaningful, always remember following thinking rountine:
    1. How can I get the current array's root?
    1.1 iff preorder series is given, we get it from the first node in the series.
    1.2 iff postorder series is given, we get it form the last node in the series.

    2. How can I divide the arrays into two parts according to the root?
    2.1 iff inorder series is given, we can get the index of the root in it (usually use HashMap).
    Then the left sub-tree's elements(in inorder series) is [inorder_low, index - 1]
    Then the right sub-tree's elements(in inorder series) is [index, inorder_high]

    3. How to get the right partion in preorder/postorder series. (which we must use in each level)
    Key: whether in preorder or postorder, all nodes belonged to left-sub tree must appear before all nodes belonged to right-sub tree. Even the orders in them may be different, we can use the length to make proper boundary.
    In postorder's case. (note: we chop off the last element)
    Inoder series:

    3.1 left sub-tree: inorder[inoder_low, index - 1], 
    3.2 right sub-tree: inorder[index + 1, inorder_high]

    Postorder series: 

    left_high - postorder_low = index - 1 - inorder_low ===> left_high = postorder_low + index - inorder_low - 1;
       
    3.3 left sub-tree: postorder[postorder_low, postorder_low + index - inorder_low - 1], 
    3.4 right sub-tree: postorder[postorder_low + index - inorder_low, postorder_high - 1] 

    My solution:

    public class Solution {
        public TreeNode buildTree(int[] inorder, int[] postorder) {
            
            if (inorder == null || inorder.length == 0)
                return null;
                
            if (postorder == null || postorder.length == 0)
                return null;
            
            int len = inorder.length;
            HashMap<Integer, Integer> order_map =  new HashMap<Integer, Integer> ();
            
            for (int i = 0; i < len; i++) {
                order_map.put(inorder[i], i);
            }
            
            return helper(inorder, 0, len - 1, postorder, 0, len - 1, order_map);
        }
        
        private TreeNode helper(int[] inorder, int in_low, int in_high, int[] postorder, int post_low, int post_high, HashMap<Integer, Integer> order_map) {
         
         if (in_high < in_low || post_high < post_low) //we use null as base case!!!
            return null;
         
         TreeNode ret = new TreeNode(postorder[post_high]);
         int index = order_map.get(postorder[post_high]);
         
         ret.left = helper(inorder, in_low, index - 1, postorder, post_low, post_low + index - in_low - 1, order_map);
         ret.right = helper(inorder, index + 1, in_high, postorder, post_low + index - in_low, post_high - 1, order_map);   
         
         return ret;
        }
    }
  • 相关阅读:
    Maven Nexus 上传jar到本地仓库。
    java Web与Flex通过HTTP service 通信 并解析Map 数据。
    JACOB调用WORD书签修改WORD文档,并生成html显示到页面中
    去除java list 中的 [] 括号
    2019-2020-2 20175222 《网络对抗技术》Exp9 Web安全基础
    2019-2020-2 20175222 《网络对抗技术》 Exp 8 Web基础
    2019-2020-2 20175222 《网络对抗技术》 Exp7 网络欺诈防范
    2019-2020-2 20175222 《网络对抗技术》 Exp6 MSF基础应用
    2019-2020-2 20175222《网络对抗技术》 Exp5 信息搜集与漏洞扫描
    2019-2020-2 20175222罗雨石 《网络对抗技术》 Exp4 恶意代码分析
  • 原文地址:https://www.cnblogs.com/airwindow/p/4210508.html
Copyright © 2011-2022 走看看