Simple Concise C# accepted solution

  • 0

    go with a DFS scan .. on every node .. compute the max value possible (whether current only or current + left or curret+right or current + both ..
    however, you should return the maximum possible path ending with the current node to your parent to consider .. which can only be any of the past four possibilities except the self + left + right ..


    public class Solution {
    int max = Int32.MinValue;
    public int MaxPathSum(TreeNode root) {
        return max;
    private int func(TreeNode root)
        if(root == null) return 0;
        int left = func(root.left);
        int right = func(root.right);
        int ret = left + root.val + right;
        max = Math.Max(max, Math.Max(root.val, Math.Max(root.val + left, Math.Max(root.val + right, root.val + left + right))));
        return Math.Max(root.val, Math.Max(left, right) + root.val);


Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.