{"id":22513466,"url":"https://github.com/emahtab/path-sum-iii","last_synced_at":"2026-03-19T23:02:59.478Z","repository":{"id":79525762,"uuid":"432223614","full_name":"eMahtab/path-sum-iii","owner":"eMahtab","description":null,"archived":false,"fork":false,"pushed_at":"2024-10-18T04:28:16.000Z","size":51,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-26T11:24:29.359Z","etag":null,"topics":["leetcode","path-sum","prefix-sum","recursion"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eMahtab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2021-11-26T15:28:58.000Z","updated_at":"2024-10-18T04:33:19.000Z","dependencies_parsed_at":"2025-07-27T20:50:13.424Z","dependency_job_id":null,"html_url":"https://github.com/eMahtab/path-sum-iii","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/eMahtab/path-sum-iii","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fpath-sum-iii","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fpath-sum-iii/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fpath-sum-iii/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fpath-sum-iii/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/path-sum-iii/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fpath-sum-iii/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29585548,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-18T13:56:48.962Z","status":"ssl_error","status_checked_at":"2026-02-18T13:54:34.145Z","response_time":162,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["leetcode","path-sum","prefix-sum","recursion"],"created_at":"2024-12-07T03:12:26.090Z","updated_at":"2026-02-18T16:31:35.273Z","avatar_url":"https://github.com/eMahtab.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Path Sum III\n\n## https://leetcode.com/problems/path-sum-iii\n\nGiven the root of a binary tree and an integer targetSum, return the number of paths where the sum of the values along the path equals targetSum.\n\nThe path does not need to start or end at the root or a leaf, but it must go downwards (i.e., traveling only from parent nodes to child nodes).\n\n![Path Sum III](example.JPG?raw=true)\n\n\n# Implementation 1a : O(n^2)\n```java\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n *     int val;\n *     TreeNode left;\n *     TreeNode right;\n *     TreeNode() {}\n *     TreeNode(int val) { this.val = val; }\n *     TreeNode(int val, TreeNode left, TreeNode right) {\n *         this.val = val;\n *         this.left = left;\n *         this.right = right;\n *     }\n * }\n */\nclass Solution {\n   public int pathSum(TreeNode root, int sum) {\n        if(root == null)\n            return 0;\n        return pathSum(root.left, sum) + pathSum(root.right, sum) + findPath(root, sum);\n   }\n\n    public int findPath(TreeNode node, int target){\n        int numberOfPathFound = 0;\n        if(node.val == target)\n            numberOfPathFound++;\n        if(node.left != null)\n            numberOfPathFound += findPath(node.left, target - node.val);\n        if(node.right != null)    \n            numberOfPathFound += findPath(node.right, target - node.val);\n        \n        return numberOfPathFound;\n    }\n}\n```\n\n## Implementation 1b : O(n^2) Making leetcode happy (when node.val is very large, target-node.val can lead to integer overflow)\n```java\nclass Solution {\n   public int pathSum(TreeNode root, int sum) {\n        if(root == null)\n            return 0;\n        return pathSum(root.left, sum) + pathSum(root.right, sum) + findPath(root, sum);\n   }\n\n    public int findPath(TreeNode node, long target){\n        int numberOfPathFound = 0;\n        if(node.val == target)\n            numberOfPathFound++;\n        if(node.left != null)\n            numberOfPathFound += findPath(node.left, target - node.val);\n        if(node.right != null)    \n            numberOfPathFound += findPath(node.right, target - node.val);\n        \n        return numberOfPathFound;\n    }\n}\n```\n\n# Implementation 2 : O(n) Using Prefix Sum\n```java\nclass Solution {\n    public int pathSum(TreeNode root, int targetSum) {\n        Map\u003cLong, Integer\u003e map = new HashMap\u003c\u003e();\n        map.put(0L, 1);\n        return helper(root, (long) targetSum, 0, map);\n    }\n\n    private int helper(TreeNode node, long targetSum, long prefixSum, Map\u003cLong, Integer\u003e map) {\n        if (node == null) {\n            return 0;\n        }\n        prefixSum += node.val;\n        int count = map.getOrDefault(prefixSum - targetSum, 0);\n        map.put(prefixSum, map.getOrDefault(prefixSum, 0) + 1);\n        count += helper(node.left, targetSum, prefixSum, map) + helper(node.right, targetSum, prefixSum, map);\n        map.put(prefixSum, map.get(prefixSum) - 1);\n        return count;\n    }\n}\n```\n\n\n# References :\n1. https://www.youtube.com/watch?v=uZzvivFkgtM\n2. https://github.com/ojasmaru/LetsAlgoTogether/blob/master/Path%20Sum%20III/Java/QuickStart.java\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fpath-sum-iii","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Fpath-sum-iii","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fpath-sum-iii/lists"}