Simple Java DFS
Typical recursive DFS.\nSpace: O(n) due to recursion.\nTime: O(n^2) in worst case (no branching); O(nlogn) in best case (balanced tree).\n\npublic class Solutio
Typical recursive DFS.\nSpace: O(n) due to recursion.\nTime: O(n^2) in worst case (no branching); O(nlogn) in best case (balanced tree).\n```\npublic class Solution {\n    public int pathSum(TreeNode root, int sum) {\n        if (root == null) return 0;\n        return pathSumFrom(root, sum) + pathSum(root.left, sum) + pathSum(root.right, sum);\n    }\n    \n    private int pathSumFrom(TreeNode node, int sum) {\n        if (node == null) return 0;\n        return (node.val == sum ? 1 : 0) \n            + pathSumFrom(node.left, sum - node.val) + pathSumFrom(node.right, sum - node.val);\n    }\n}\n```
[]