427.Path Sum Posted on 2017-09-13 | In LeetCode Solution 1: accepted 30msRecursive DFS. Check all possible paths from each node.Time: O(n^2)Space: O(n) 12345678910111213141516public class Solution { public int pathSum(TreeNode root, int sum) { if (root == null) return 0; return rootSum(root, sum) + pathSum(root.left, sum) + pathSum(root.right, sum); } // Check all paths starts from this node private int rootSum (TreeNode root, int sum) { int result = 0; if (root == null) return result; if (sum == root.val) result++; result += rootSum(root.left, sum - root.val); result += rootSum(root.right, sum - root.val); return result; }}