• [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;
        }
    }
  • 相关阅读:
    swiper 增加一个鼠标移入分页器的小点后就切换展示图片
    css中的单冒号和双冒号 以及 伪类和伪元素
    pointer-events: none;元素永远不会成为鼠标事件的target
    jQuery off() 方法
    jQuery方法汇总
    vue 数组修改 页面无法刷新
    mysql error Code 1441:datetime function: datetime field overflow
    生命的意义
    删除镜像或容器
    nginx Redis 不能访问问题
  • 原文地址:https://www.cnblogs.com/airwindow/p/4210508.html
Copyright © 2020-2023  润新知