clueless coding // TODO: be smarter

LeetCode Path Sum III: Java Solution



TL;DR Code Solution


public int pathSum(TreeNode root, int sum) {

    if (root == null) {
        return 0;
    }

    return pathSum(root.left, sum) + pathSum(root.right, sum) + find(root, sum);


}

public int find(TreeNode root, int sum) {

    int result = 0;
    if (root == null)
        return 0;

    if (sum == root.val)
        result++;

    result += find(root.left, sum - root.val);
    result += find(root.right, sum - root.val);

    return result;

}