{"id":22513632,"url":"https://github.com/emahtab/path-sum-ii","last_synced_at":"2026-01-07T12:04:38.373Z","repository":{"id":79525713,"uuid":"398443897","full_name":"eMahtab/path-sum-ii","owner":"eMahtab","description":null,"archived":false,"fork":false,"pushed_at":"2025-01-15T09:03:56.000Z","size":27,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-02T03:21:25.202Z","etag":null,"topics":["dfs","leetcode","tree"],"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}},"created_at":"2021-08-21T02:03:58.000Z","updated_at":"2025-01-15T09:03:57.000Z","dependencies_parsed_at":"2023-05-10T17:30:32.928Z","dependency_job_id":null,"html_url":"https://github.com/eMahtab/path-sum-ii","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fpath-sum-ii","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fpath-sum-ii/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fpath-sum-ii/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fpath-sum-ii/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/path-sum-ii/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245950814,"owners_count":20699143,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["dfs","leetcode","tree"],"created_at":"2024-12-07T03:13:47.380Z","updated_at":"2026-01-07T12:04:38.344Z","avatar_url":"https://github.com/eMahtab.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Path Sum II\n\n## https://leetcode.com/problems/path-sum-ii\n\nGiven the root of a binary tree and an integer targetSum, return all root-to-leaf paths where the sum of the node values in the path equals targetSum. Each path should be returned as a list of the node values, not node references.\n\nA root-to-leaf path is a path starting from the root and ending at any leaf node. A leaf is a node with no children.\n\n![Sum of Root to Leaf nodes equals to target Sum](pathsum-ii.jpg?raw=true)\n\n# Implementation 1 : DFS\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 List\u003cList\u003cInteger\u003e\u003e pathSum(TreeNode root, int targetSum) {\n        List\u003cList\u003cInteger\u003e\u003e result = new ArrayList\u003c\u003e();\n        if(root == null)\n            return result;\n        List\u003cInteger\u003e path = new ArrayList\u003c\u003e();\n        traverse(root, targetSum, result, path);\n        return result;\n    }\n    \n    private void traverse(TreeNode node, int targetSum, List\u003cList\u003cInteger\u003e\u003e result, List\u003cInteger\u003e path) {\n        path.add(node.val);\n        if(node.left == null \u0026\u0026 node.right == null \u0026\u0026 node.val == targetSum) {\n             result.add(path);\n             return;\n        }\n        if(node.left != null) {\n            traverse(node.left, targetSum - node.val, result, new ArrayList\u003cInteger\u003e(path));\n        }\n        if(node.right != null)\n            traverse(node.right, targetSum - node.val, result, new ArrayList\u003cInteger\u003e(path));\n            \n    }\n}\n\n```\n## Implementation 2 : DFS with Backtracking\n\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 List\u003cList\u003cInteger\u003e\u003e pathSum(TreeNode root, int targetSum) {\n        List\u003cList\u003cInteger\u003e\u003e result = new ArrayList\u003c\u003e();\n        if(root == null)\n          return result;\n        traversePath(root, targetSum, 0, result, new ArrayList\u003cInteger\u003e());\n        return result;\n    }\n\n    private void traversePath(TreeNode node, int targetSum, int runningSum, List\u003cList\u003cInteger\u003e\u003e result,            List\u003cInteger\u003e path) {\n        runningSum += node.val;\n        path.add(node.val);\n        if(runningSum == targetSum \u0026\u0026 node.left == null \u0026\u0026 node.right == null) {\n            result.add(new ArrayList\u003cInteger\u003e(path));\n        }\n        if(node.left != null)\n        traversePath(node.left, targetSum, runningSum, result, path);\n        if(node.right != null)\n        traversePath(node.right, targetSum, runningSum, result, path);\n        path.remove(path.size()-1);\n    }\n}\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fpath-sum-ii","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Fpath-sum-ii","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fpath-sum-ii/lists"}