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;
        }
    }
  • 相关阅读:
    input 标签取消readOnly属性
    python selenium 跑脚本的时候按钮无法点击的解决办法
    Python Selenium 调用IE浏览器失败Unexpected error launching Internet Explorer解决方法
    转载--Python random模块(获取随机数)常用方法和使用例子
    转载--python selenium实现文件、图片上传
    ieDriver启动IE浏览器显示This is the initial start page for the WebDriver server的解决办法
    自动化测试用例设计学习心得总结
    关于selene安装插件ide不能识别插件的问题解决办法
    cmd 启动mysql
    最大子序列
  • 原文地址:https://www.cnblogs.com/airwindow/p/4210508.html
Copyright © 2011-2022 走看看