|
| 1 | +--- |
| 2 | +id: 117-populating-next-right-pointer-2 |
| 3 | +title: Populating Next Right Pointer To Each Node II |
| 4 | +sidebar_label: 0117 Populating Next Right Pointer To Each Node II |
| 5 | +tags: |
| 6 | + - Java |
| 7 | + - Python |
| 8 | + - C++ |
| 9 | +description: "Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL." |
| 10 | +--- |
| 11 | + |
| 12 | +## Problem Description |
| 13 | + |
| 14 | +Given 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 node values, and paths are represented as lists of node values. |
| 15 | + |
| 16 | + |
| 17 | +### Examples |
| 18 | + |
| 19 | +**Example 1:** |
| 20 | + |
| 21 | + |
| 22 | +``` |
| 23 | +Input: root = [1,2,3,4,5,null,7] |
| 24 | +Output: [1,#,2,3,#,4,5,7,#] |
| 25 | +``` |
| 26 | + |
| 27 | +**Example 2:** |
| 28 | + |
| 29 | + |
| 30 | +``` |
| 31 | +Input: root = [] |
| 32 | +Output: [] |
| 33 | +``` |
| 34 | + |
| 35 | +### Constraints |
| 36 | + |
| 37 | +- The number of nodes in the tree is in the range [0, 6000]. |
| 38 | +- $-100 <= Node.val <= 100$ |
| 39 | +--- |
| 40 | + |
| 41 | +## Solution for Binary Tree Problem |
| 42 | + |
| 43 | +### Intuition And Approach |
| 44 | + |
| 45 | +To find all root-to-leaf paths where the sum equals targetSum, we can use Depth-First Search (DFS). We'll traverse the tree and keep track of the current path and its sum. When a leaf node is reached, we check if the current path's sum equals targetSum. If it does, we add the path to our result list. |
| 46 | + |
| 47 | +<Tabs> |
| 48 | + <tabItem value="Recursive" label="Recursive"> |
| 49 | + |
| 50 | + |
| 51 | +#### Code in Different Languages |
| 52 | + |
| 53 | +<Tabs> |
| 54 | + <TabItem value="Java" label="Java" default> |
| 55 | + <SolutionAuthor name="@Vipullakum007"/> |
| 56 | + ```java |
| 57 | + private void dfs(TreeNode node, int sum, List<Integer> path, List<List<Integer>> result) { if (node == null) return; path.add(node.val); if (node.left == null && node.right == null && node.val == sum) { result.add(new ArrayList<>(path)); } else { dfs(node.left, sum - node.val, path, result); dfs(node.right, sum - node.val, path, result); } path.remove(path.size() - 1); } |
| 58 | + ``` |
| 59 | + |
| 60 | + </TabItem> |
| 61 | + <TabItem value="Python" label="Python"> |
| 62 | + <SolutionAuthor name="@Vipullakum007"/> |
| 63 | + ```python |
| 64 | + class Solution: def pathSum(self, root: TreeNode, targetSum: int) -> List[List[int]]: def dfs(node, current_path, current_sum): if not node: return current_path.append(node.val) current_sum += node.val if not node.left and not node.right and current_sum == targetSum: result.append(list(current_path)) else: dfs(node.left, current_path, current_sum) dfs(node.right, current_path, current_sum) current_path.pop() |
| 65 | + ``` |
| 66 | + |
| 67 | + </TabItem> |
| 68 | + <TabItem value="C++" label="C++"> |
| 69 | + <SolutionAuthor name="@Vipullakum007"/> |
| 70 | + ```cpp |
| 71 | + void dfs(TreeNode* node, int sum, vector<int>& path, vector<vector<int>>& result) { if (!node) return; path.push_back(node->val); if (!node->left && !node->right && node->val == sum) { result.push_back(path); } else { dfs(node->left, sum - node->val, path, result); dfs(node->right, sum - node->val, path, result); } path.pop_back(); } |
| 72 | + ``` |
| 73 | + |
| 74 | + </TabItem> |
| 75 | +</Tabs> |
| 76 | + |
| 77 | +#### Complexity Analysis |
| 78 | + |
| 79 | +- Time Complexity: $O(n)$ where n is the number of nodes in the binary tree. |
| 80 | +- Space Complexity: $O(h)$ where h is the height of the binary tree. |
| 81 | + |
| 82 | +</tabItem> |
| 83 | +</Tabs> |
| 84 | + |
| 85 | + |
| 86 | +--- |
0 commit comments