`{"id":"d437c572-5482-40ef-9cdf-dea47ef76929","name":"Path Sum In Binary Tree - Ii","description":"1. You are given a partially written function to solve.\r\n2. You are required to complete the body of PathSum function. The function is expected to return all root-to-leaf paths where each path's sum equals targetSum.\r\n3. Input and Output is managed for you.\r\n","inputFormat":"Input is managed for you.","outputFormat":"Output is managed for you. ","constraints":"0 &lt;= Number of Nodes &lt;= 10^9\r\n-10^9 &lt;= value of Node data &lt;= 10^9\r\n","sampleCode":{"cpp":{"code":"#include <iostream>\r\n#include <vector>\r\nusing namespace std;\r\n\r\nclass TreeNode\r\n{\r\npublic:\r\n int val = 0;\r\n TreeNode* left = nullptr;\r\n TreeNode* right = nullptr;\r\n\r\n TreeNode(int val)\r\n {\r\n this->val = val;\r\n }\r\n};\r\n\r\nvector<vector<int>> pathSum(TreeNode* root, int targetSum)\r\n{\r\n return {};\r\n}\r\n\r\n// input_Section=================================================\r\n\r\nTreeNode* createTree(vector<int>& arr, vector<int>& IDX)\r\n{\r\n if (IDX > arr.size() || arr[IDX] == -1)\r\n {\r\n IDX++;\r\n return nullptr;\r\n }\r\n TreeNode* node = new TreeNode(arr[IDX++]);\r\n node->left = createTree(arr, IDX);\r\n node->right = createTree(arr, IDX);\r\n\r\n return node;\r\n}\r\n\r\nvoid solve()\r\n{\r\n int n;\r\n cin >> n;\r\n vector<int> arr(n, 0);\r\n for (int i = 0; i < n; i++)\r\n {\r\n cin >> arr[i];\r\n }\r\n int tar;\r\n cin >> tar;\r\n vector<int> IDX(1, 0);\r\n TreeNode* root = createTree(arr, IDX);\r\n vector<vector<int>> ans = pathSum(root, tar);\r\n for (auto ar : ans)\r\n {\r\n for (int ele : ar)\r\n {\r\n cout << ele << \" \";\r\n }\r\n cout << endl;\r\n }\r\n}\r\n\r\nint main()\r\n{\r\n solve();\r\n return 0;\r\n}"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n public static Scanner scn = new Scanner(System.in);\r\n\r\n public static class TreeNode {\r\n int val = 0;\r\n TreeNode left = null;\r\n TreeNode right = null;\r\n\r\n TreeNode(int val) {\r\n this.val = val;\r\n }\r\n }\r\n\r\n public static ArrayList<ArrayList<Integer>> pathSum(TreeNode root, int targetSum) {\r\n return null;\r\n }\r\n\r\n // input_Section=================================================\r\n\r\n public static TreeNode createTree(int[] arr, int[] IDX) {\r\n if (IDX > arr.length || arr[IDX] == -1) {\r\n IDX++;\r\n return null;\r\n }\r\n\r\n TreeNode node = new TreeNode(arr[IDX++]);\r\n node.left = createTree(arr, IDX);\r\n node.right = createTree(arr, IDX);\r\n\r\n return node;\r\n }\r\n\r\n public static void solve() {\r\n int n = scn.nextInt();\r\n int[] arr = new int[n];\r\n for (int i = 0; i < n; i++)\r\n arr[i] = scn.nextInt();\r\n int tar = scn.nextInt();\r\n int[] IDX = new int;\r\n TreeNode root = createTree(arr, IDX);\r\n ArrayList<ArrayList<Integer>> ans = pathSum(root, tar);\r\n for (ArrayList<Integer> ar : ans) {\r\n for (Integer ele : ar) {\r\n System.out.print(ele + \" \");\r\n }\r\n System.out.println();\r\n }\r\n }\r\n\r\n public static void main(String[] args) {\r\n solve();\r\n }\r\n}"},"ruby":{"code":""},"python":{"code":""},"javascript":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"7\r\n1\r\n2\r\n-1\r\n-1\r\n3\r\n-1\r\n-1\r\n4","sampleOutput":"1 3 \r\n","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"2e9df04c-be14-441c-9a18-8b5a8ca6596d","name":"Trees For Intermediate","slug":"trees-for-intermediate-9994","type":0},{"id":"29eed80d-4319-44d4-ac1f-62cce9ca855e","name":"Path Sum In Binary Tree - Ii","slug":"path-sum-in-binary-tree-ii","type":1}],"next":{"id":"6b566c91-1b27-4f1d-bc05-93dc30b14883","name":"path sum in binary tree 2","type":3,"slug":"path-sum-in-binary-tree-2"},"prev":{"id":"629e2d7f-448c-431d-b0b2-12ea96907650","name":"path sum in binary tree","type":3,"slug":"path-sum-in-binary-tree"}}}`

# Path Sum In Binary Tree - Ii

1. You are given a partially written function to solve. 2. You are required to complete the body of PathSum function. The function is expected to return all root-to-leaf paths where each path's sum equals targetSum. 3. Input and Output is managed for you.

`{"id":"d437c572-5482-40ef-9cdf-dea47ef76929","name":"Path Sum In Binary Tree - Ii","description":"1. You are given a partially written function to solve.\r\n2. You are required to complete the body of PathSum function. The function is expected to return all root-to-leaf paths where each path's sum equals targetSum.\r\n3. Input and Output is managed for you.\r\n","inputFormat":"Input is managed for you.","outputFormat":"Output is managed for you. ","constraints":"0 &lt;= Number of Nodes &lt;= 10^9\r\n-10^9 &lt;= value of Node data &lt;= 10^9\r\n","sampleCode":{"cpp":{"code":"#include <iostream>\r\n#include <vector>\r\nusing namespace std;\r\n\r\nclass TreeNode\r\n{\r\npublic:\r\n int val = 0;\r\n TreeNode* left = nullptr;\r\n TreeNode* right = nullptr;\r\n\r\n TreeNode(int val)\r\n {\r\n this->val = val;\r\n }\r\n};\r\n\r\nvector<vector<int>> pathSum(TreeNode* root, int targetSum)\r\n{\r\n return {};\r\n}\r\n\r\n// input_Section=================================================\r\n\r\nTreeNode* createTree(vector<int>& arr, vector<int>& IDX)\r\n{\r\n if (IDX > arr.size() || arr[IDX] == -1)\r\n {\r\n IDX++;\r\n return nullptr;\r\n }\r\n TreeNode* node = new TreeNode(arr[IDX++]);\r\n node->left = createTree(arr, IDX);\r\n node->right = createTree(arr, IDX);\r\n\r\n return node;\r\n}\r\n\r\nvoid solve()\r\n{\r\n int n;\r\n cin >> n;\r\n vector<int> arr(n, 0);\r\n for (int i = 0; i < n; i++)\r\n {\r\n cin >> arr[i];\r\n }\r\n int tar;\r\n cin >> tar;\r\n vector<int> IDX(1, 0);\r\n TreeNode* root = createTree(arr, IDX);\r\n vector<vector<int>> ans = pathSum(root, tar);\r\n for (auto ar : ans)\r\n {\r\n for (int ele : ar)\r\n {\r\n cout << ele << \" \";\r\n }\r\n cout << endl;\r\n }\r\n}\r\n\r\nint main()\r\n{\r\n solve();\r\n return 0;\r\n}"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n public static Scanner scn = new Scanner(System.in);\r\n\r\n public static class TreeNode {\r\n int val = 0;\r\n TreeNode left = null;\r\n TreeNode right = null;\r\n\r\n TreeNode(int val) {\r\n this.val = val;\r\n }\r\n }\r\n\r\n public static ArrayList<ArrayList<Integer>> pathSum(TreeNode root, int targetSum) {\r\n return null;\r\n }\r\n\r\n // input_Section=================================================\r\n\r\n public static TreeNode createTree(int[] arr, int[] IDX) {\r\n if (IDX > arr.length || arr[IDX] == -1) {\r\n IDX++;\r\n return null;\r\n }\r\n\r\n TreeNode node = new TreeNode(arr[IDX++]);\r\n node.left = createTree(arr, IDX);\r\n node.right = createTree(arr, IDX);\r\n\r\n return node;\r\n }\r\n\r\n public static void solve() {\r\n int n = scn.nextInt();\r\n int[] arr = new int[n];\r\n for (int i = 0; i < n; i++)\r\n arr[i] = scn.nextInt();\r\n int tar = scn.nextInt();\r\n int[] IDX = new int;\r\n TreeNode root = createTree(arr, IDX);\r\n ArrayList<ArrayList<Integer>> ans = pathSum(root, tar);\r\n for (ArrayList<Integer> ar : ans) {\r\n for (Integer ele : ar) {\r\n System.out.print(ele + \" \");\r\n }\r\n System.out.println();\r\n }\r\n }\r\n\r\n public static void main(String[] args) {\r\n solve();\r\n }\r\n}"},"ruby":{"code":""},"python":{"code":""},"javascript":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"7\r\n1\r\n2\r\n-1\r\n-1\r\n3\r\n-1\r\n-1\r\n4","sampleOutput":"1 3 \r\n","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"2e9df04c-be14-441c-9a18-8b5a8ca6596d","name":"Trees For Intermediate","slug":"trees-for-intermediate-9994","type":0},{"id":"29eed80d-4319-44d4-ac1f-62cce9ca855e","name":"Path Sum In Binary Tree - Ii","slug":"path-sum-in-binary-tree-ii","type":1}],"next":{"id":"6b566c91-1b27-4f1d-bc05-93dc30b14883","name":"path sum in binary tree 2","type":3,"slug":"path-sum-in-binary-tree-2"},"prev":{"id":"629e2d7f-448c-431d-b0b2-12ea96907650","name":"path sum in binary tree","type":3,"slug":"path-sum-in-binary-tree"}}}` Editor

# Path Sum In Binary Tree - Ii

medium

1. You are given a partially written function to solve. 2. You are required to complete the body of PathSum function. The function is expected to return all root-to-leaf paths where each path's sum equals targetSum. 3. Input and Output is managed for you.

## Constraints

0 <= Number of Nodes <= 10^9 -10^9 <= value of Node data <= 10^9

## Format

### Input

Input is managed for you.

### Output

Output is managed for you.

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}7 1 2 -1 -1 3 -1 -1 4```

### Sample Output

```.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}1 3 ```

Discussions

Show Discussion

Related Resources 