Wrong test case?


  • -1
    R

    Below is my code and the test case is as below, would admin please confirm, in no way it can output 6, and i have rerun on visual studio, the result is 0?

    public class Solution {
    private static int maxValue = int.MinValue;
    public int MaxPathSum(TreeNode root) {
    MaxPathSumHelper(root);
    return Solution.maxValue;
    }

    int MaxPathSumHelper(TreeNode root)
    {
        if (root == null) return 0;
        if (root.left == null && root.right == null)
        {
            Solution.maxValue = Math.Max(maxValue, root.val);
            return root.val;
        }
        
        int leftValue = MaxPathSumHelper(root.left);
        int rightValue = MaxPathSumHelper(root.right);
        
        int max = Math.Max(root.val, root.val + leftValue + rightValue);
        Solution.maxValue = Math.Max(max, Solution.maxValue);
        
        return root.val + leftValue + rightValue;
    }
    

    Input:
    [0]
    Output:
    6
    Expected:
    0
    }


Log in to reply
 

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