zoukankan      html  css  js  c++  java
  • [LintCode] Binary Tree Maximum Path Sum

    Given a binary tree, find the maximum path sum.

    The path may start and end at any node in the tree.

    Example

    Given the below binary tree:

      1
     / 
    2   3
    

    return 6.

    Given a binary tree data structure, we should instinctly consider recursion. 

    The maximum path sum must at least include one node from the input binary tree. 

    For any node t, we consider it as the root of its subtree.  The maximum path sum 

    either includes the root node t or does not include t.  If the max path sum does not 

    include t, then the max path is either the max path of t's left subtree or the max path

    of t's right subtree.  If the max path sum includes t, then the max path contains a 

    straight path from t's left subtree, node t, then a straight path from t's right sub tree. 

    Optimal Substructure:

    maxPathSum(t) = Math.max(Math.max(maxPathSum(t.left), maxPathSum(t.right)),    //max path sum does not include t

                   Math.max(left.straightPath, 0) + node.val + Math.max(right.straightPath, 0)); //max path sum includes t

    Solution 1. Top down recursively memoization search 

    From the above formula, we know for a given node, we need its left/right subtrees' straight paths

    and max paths to calculate the max path sum at the given node. Instead of redundantly compute subproblems' 

    straight paths and max paths, we can use  the same top down memoization search approach as used in House Robber III.

     1 /**
     2  * Definition of TreeNode:
     3  * public class TreeNode {
     4  *     public int val;
     5  *     public TreeNode left, right;
     6  *     public TreeNode(int val) {
     7  *         this.val = val;
     8  *         this.left = this.right = null;
     9  *     }
    10  * }
    11  */
    12 public class Solution {
    13     private class ResultType {
    14         protected int singlePath;
    15         protected int maxPath;
    16         protected ResultType(int singlePath, int maxPath){
    17             this.singlePath = singlePath;
    18             this.maxPath = maxPath;
    19         }
    20     }
    21     private ResultType helper(TreeNode node){
    22         if(node == null){
    23             return new ResultType(0, Integer.MIN_VALUE);
    24         }
    25         
    26         ResultType left = helper(node.left);
    27         ResultType right = helper(node.right);
    28         
    29         int singlePath = Math.max(0, Math.max(left.singlePath, right.singlePath)) + node.val;
    30         int maxPathWithOutCurrentNode = Math.max(left.maxPath, right.maxPath);
    31         int maxPathWithCurrentNode = Math.max(left.singlePath, 0) + node.val + Math.max(right.singlePath, 0);
    32         int maxPath = Math.max(maxPathWithOutCurrentNode, maxPathWithCurrentNode);
    33         
    34         return new ResultType(singlePath, maxPath);
    35     }
    36     public int maxPathSum(TreeNode root) {
    37         ResultType result = helper(root);
    38         return result.maxPath;
    39     }
    40 }

    Follow up question:  Can you show how solution 1 avoids duplicated computations compared to a brute force recursive solution without memoization?

    Solution 2. Brute force recursive approach, TLE

     1 public class Solution {
     2     public int maxPathSum(TreeNode root) {
     3         if(root == null){
     4             return Integer.MIN_VALUE;
     5         }    
     6         int leftMaxPathSum = maxPathSum(root.left);
     7         int rightMaxPathSum = maxPathSum(root.right);
     8         int maxPathWithOutCurrentNode = Math.max(leftMaxPathSum, rightMaxPathSum);
     9         int leftMaxSinglePathSum = maxSinglePathSum(root.left);
    10         int rightMaxSinglePathSum = maxSinglePathSum(root.right);
    11         int maxPathWithCurrentNode = Math.max(leftMaxSinglePathSum, 0) 
    12                                     + root.val 
    13                                     + Math.max(rightMaxSinglePathSum, 0);   
    14         return Math.max(maxPathWithOutCurrentNode, maxPathWithCurrentNode);
    15     }
    16     private int maxSinglePathSum(TreeNode node){
    17         if(node == null){
    18             return 0;
    19         }
    20         int leftMaxSinglePathSum = maxSinglePathSum(node.left);
    21         int rightMaxSinglePathSum = maxSinglePathSum(node.right);
    22         return Math.max(0, Math.max(leftMaxSinglePathSum, rightMaxSinglePathSum)) + node.val;
    23     }
    24 }

    Compare the red highlighted section of both solutions.  In the brute force solution, every time we need to calculate the 

    max single path sum of a given node t, we traverse t's left subtree and right subtree.  To calculate t.left and t.right's

    max single path sum, we traverse t.left's subtree and t.right's subtree, which are also part of t's subtree.

    If we use memoization and store t.left and t.right's max single path sum, we can compute t's max single path sum in O(1) time.

    If we just use the brute force approach, we compute t's max single path sum in O(2^(t's height)) time.

    Related Problems

    House Robber III

  • 相关阅读:
    Atitit opencv3.0  3.1 3.2 新特性attilax总结
    Atitit html5.1 新特性attilax总结
    Atitit http2 新特性
    Atitit 大龄软件工程师的出路attilax总结
    Atitit 软件项目系统托盘图标解决方案
    Atitit js canvas的图像处理类库attilax总结与事业
    Atitit 切入一个领域的方法总结 attilax这里,机器学习为例子
    css知多少(8)——float上篇
    css知多少(7)——盒子模型
    css知多少(6)——选择器的优先级
  • 原文地址:https://www.cnblogs.com/lz87/p/6955073.html
Copyright © 2011-2022 走看看