Help with a C# answer?


  • 0
    Z

    I'm trying to debug this solution, but am returning 1 instead of 2 on this input. I'm not seeing what's going wrong here. Thanks in advance.

    [1,null,2,null,3,null,4,null,5]
    3

    public class Solution {
        public int PathSum(TreeNode root, int sum) 
        {
            return root == null ? 0 : findLists(root, sum) + findLists(root.left, sum) + findLists(root.right, sum);
        }
        
        public int findLists(TreeNode node, int sum)
        {
            int result = 0;
            if (node == null) return result;
            if (sum == node.val) result++; 
            return result + findLists(node.left, sum - node.val) + findLists(node.right, sum - node.val);
        }
    }
    

Log in to reply
 

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