Easy to understand Java solution with comment.
Any suggestion is greatly appreciated.\n```\n/\n * Definition for a binary tree node.\n * public class TreeNode {\n *     int val;\n *     TreeNode left;\n *   
Any suggestion is greatly appreciated.\n```\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n *     int val;\n *     TreeNode left;\n *     TreeNode right;\n *     TreeNode(int x) { val = x; }\n * }\n */\n \n/*\nfor each parent node in the tree, we have 2 choices:\n1. include it in the path to reach sum.\n2. not include it in the path to reach sum. \n\nfor each child node in the tree, we have 2 choices:\n1. take what your parent left you.\n2. start from yourself to form the path.\n\none little thing to be careful:\nevery node in the tree can only try to be the start point once.\n\nfor example, When we try to start with node 1, node 3, as a child, could choose to start by itself.\n             Later when we try to start with 2, node 3, still as a child, \n             could choose to start by itself again, but we don't want to add the count to result again.\n     1\n      \\\n       2\n        \\\n         3\n         \n*/ \npublic class Solution {\n    int target;\n    Set<TreeNode> visited;\n    public int pathSum(TreeNode root, int sum) {\n        target = sum;\n        visited = new HashSet<TreeNode>();  // to store the nodes that have already tried to start path by themselves.\n        return pathSumHelper(root, sum, false);\n    }\n    \n    public int pathSumHelper(TreeNode root, int sum, boolean hasParent) {\n        if(root == null) return 0;\n        //the hasParent flag is used to handle the case when parent path sum is 0.\n        //in this case we still want to explore the current node.\n        if(sum == target && visited.contains(root) && !hasParent) return 0;\n        if(sum == target && !hasParent) visited.add(root);\n        int count = (root.val == sum)?1:0;\n        count += pathSumHelper(root.left, sum - root.val, true);\n        count += pathSumHelper(root.right, sum - root.val, true);\n        count += pathSumHelper(root.left, target , false);\n        count += pathSumHelper(root.right, target, false);\n        return count;\n    }\n}````
[]