public static int maxPathSum_B(TreeNode root) { int max=Int32.MinValue; dfs(root, ref max); return max; }
/** * Latest update: July 5, 2015 * Leetcode: * Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example: Given the below binary tree, 1 / \ 2 3 Return 6. * source code copied from: https://github.com/xiaoxq/leetcode-cpp/blob/master/src/BinaryTreeMaximumPathSum.cpp * */ // Definition for binary tree public static int maxPathSum_A(TreeNode root) { if( root==null ) return 0; int result = root.val; int sumWithRoot = maxPathSum( root, ref result ); return Math.Max( sumWithRoot, result ); }
/** * Latest update: July 5, 2015 * * source code from: * http://jane4532.blogspot.ca/2013/09/binary-tree-maximum-path-sumleetcode.html * comment from blog author: * We need to pay attention on 3 points in this problem: 1. Binary tree 2. It can start and end in any place. 3. The node value can be positive, 0 and negative. * The main idea is recursion and DFS. Analyse the problem, when we are in a root node, * how we decided to include the subtree in the maximum * path sum? So we need to calculate the max path sum * in both subtree. In the meantime, we need to set * a variable "max" to store the max path sum we found * during the recursion. Pay attention to one thing is, * when a root's value is negative, we need to decide * include it or discard it. * * Julia's comment: * 1. It is much easy to understand the base case. * 2. It passed leetcode online judge. */ public static int dfs(TreeNode root, ref int max) { if(root==null) return 0; int l=dfs(root.left, ref max); int r=dfs(root.right,ref max); int m=root.val; if(l>0) m+=l; if(r>0) m+=r; if(m>max) max=m; return Math.Max(l,r)>0? Math.Max(l,r)+root.val : root.val ; }
protected static int maxPathSum(TreeNode root, ref int result) { /* return a mini number, make sure it <= result julia comment: why result is compared to 0, and also value is -1 if result>0. What is the base case: * * * */ if( root==null ) return result < 0 ? result : -1; // julia comment: why declare constant int instead of int in C++ code (see the blog)? int left = maxPathSum(root.left, ref result); int right = maxPathSum(root.right, ref result); int maxiChild = max(left, right); int withRoot = left + right + root.val; // julia comment: maximum path sum is determined here. result = max(result, max(maxiChild, withRoot)); // julia comment: calculate the maximum value from root node to any node in the tree if( left<=0 && right<=0 ) return root.val; return maxiChild+root.val; }
public TreeNode(int x) { left = null; right = null; val = x; }
static void Main(string[] args) { /** * Test case 1: * Given the below binary tree, 1 / \ 2 3 Return 6. */ TreeNode node1 = new TreeNode(1); node1.left = new TreeNode(2); node1.right = new TreeNode(3); int result = maxPathSum_A(node1); /** * Test case 2: 1 2 3 4 5 * * 4 + 2 + 5 > 7 + 1 + 3, result is 11 */ { TreeNode n1 = new TreeNode(1); n1.left = new TreeNode(2); n1.right = new TreeNode(3); n1.left.left = new TreeNode(4); n1.left.right = new TreeNode(5); int test2 = maxPathSum_A(n1); } /** * Test case 3: 1 2 3 4 3 * * Result is 4 + 2 + 1 + 3, is 10 */ { TreeNode n1 = new TreeNode(1); n1.left = new TreeNode(2); n1.right = new TreeNode(3); n1.left.left = new TreeNode(4); n1.left.right = new TreeNode(3); int test3 = maxPathSum_A(n1); } /** * Test case 4: 5 -2 -1 * * Result is 5 */ { TreeNode n1 = new TreeNode(5); int test3 = maxPathSum_A(n1); } /** * Test case 4: 1 2 3 4 3 * * Result is 4 + 2 + 1 + 3, is 10 * The implementation (version B) is using DFS, recursive. * */ { TreeNode n1 = new TreeNode(1); n1.left = new TreeNode(2); n1.right = new TreeNode(3); n1.left.left = new TreeNode(4); n1.left.right = new TreeNode(3); int test3 = maxPathSum_B(n1); } }