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

  • 相关阅读:
    数学建模课程常用英语词汇(专业术语)
    MATLAB简易画图
    MATLAB与C语言对比实例:随机数生成
    C语言 投票系统:给定候选人,从键盘输入候选人的名字,统计票数,并输出最终获胜者
    C语言 一个数学问题:求s=(a^m)!+(b^n)!
    C语言 矩阵的转置及矩阵的乘法
    iOS_数据存取(二)
    iOS_数据存取(一)
    iOS_SDWebImage框架分析
    iOS_AFNetWorking框架分析
  • 原文地址:https://www.cnblogs.com/lz87/p/6955073.html
Copyright © 2011-2022 走看看