[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

转载于:https://www.cnblogs.com/lz87/p/6955073.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值