diff --git a/dsa-solutions/lc-solutions/0100-0199/0113-path-sum-2.md b/dsa-solutions/lc-solutions/0100-0199/0113-path-sum-2.md deleted file mode 100644 index 0868516f0..000000000 --- a/dsa-solutions/lc-solutions/0100-0199/0113-path-sum-2.md +++ /dev/null @@ -1,122 +0,0 @@ ---- -id: 113-path-sum-2 -title: Path Sum 2 -sidebar_label: 0113-Path Sum 2 -tags: - - Java - - Python - - C++ -description: "Find all root-to-leaf paths in a binary tree where the sum of the node values equals a given number." ---- - -## Problem Description - -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. - -### Examples - -**Example 1:** - -![LeetCode Problem - Binary Tree](https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg) -``` -Input: root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 -Output: [[5,4,11,2],[5,8,4,5]] -``` - -**Example 1:** - -![LeetCode Problem - Binary Tree](https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg) -``` -Input: root = [1,2,3], targetSum = 5 -Output: [] -``` - -### Constraints - -- The number of nodes in the tree is in the range [0, 105]. -- $-1000 <= Node.val <= 1000$ -- $-1000 <= targetSum <= 1000$ ---- - -## Solution for Binary Tree Problem - -### Intuition And Approach - -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. - - - - - -#### Code in Different Languages - - - - - ```java - private void dfs(TreeNode node, int sum, List path, List> 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); -} - ``` - - - - - ```python - 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() - - result = [] - dfs(root, [], 0) - return result - ``` - - - - - ```cpp - void dfs(TreeNode* node, int sum, vector& path, vector>& 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(); -} - ``` - - - - -#### Complexity Analysis - -- Time Complexity: $O(n)$ where n is the number of nodes in the binary tree. -- Space Complexity: $O(h)$ where h is the height of the binary tree. - - - - - ---- \ No newline at end of file diff --git a/dsa-solutions/lc-solutions/0100-0199/0116-populating-next-right-pointers-in-each-node.md b/dsa-solutions/lc-solutions/0100-0199/0116-populating-next-right-pointers-in-each-node.md new file mode 100644 index 000000000..c13d9d3dd --- /dev/null +++ b/dsa-solutions/lc-solutions/0100-0199/0116-populating-next-right-pointers-in-each-node.md @@ -0,0 +1,146 @@ +--- +id: 116-populating-next-right-pointers-in-each-node +title: Populating Next Right Pointers in Each Node +sidebar_label: 0116-Populating Next Right Pointers in Each Node +tags: + - Java + - Python + - C++ +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." +--- + +## Problem Description + +You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition 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. + +Initially, all next pointers are set to NULL. + +### Examples + +**Example 1:** + +![LeetCode Problem - Binary Tree](https://assets.leetcode.com/uploads/2019/02/14/116_sample.png) +``` +Input: root = [1,2,3,4,5,6,7] +Output: [1,#,2,3,#,4,5,6,7,#] +``` + +**Example 2:** + +``` +Input: root = [] +Output: [] +``` + +### Constraints + +- The number of nodes in the tree is in the range [0, 212 - 1]. +- $-1000 <= Node.val <= 1000$ + +--- + +## Solution for Binary Tree Problem + +### Intuition And Approach + +To populate each next pointer to point to its next right node in a perfect binary tree, we can perform a level order traversal and connect nodes at the same level. + + + + + +#### Code in Different Languages + + + + + ```java + class Solution { + public Node connect(Node root) { + if (root == null) return null; + Queue queue = new LinkedList<>(); + queue.offer(root); + while (!queue.isEmpty()) { + int levelSize = queue.size(); + Node prev = null; + for (int i = 0; i < levelSize; i++) { + Node curr = queue.poll(); + if (prev != null) { + prev.next = curr; + } + if (curr.left != null) queue.offer(curr.left); + if (curr.right != null) queue.offer(curr.right); + prev = curr; + } + } + return root; + } +} + ``` + + + + + ```python + class Solution: + def connect(self, root: 'Node') -> 'Node': + if not root: + return None + queue = collections.deque([root]) + while queue: + level_size = len(queue) + prev = None + for _ in range(level_size): + node = queue.popleft() + if prev: + prev.next = node + if node.left: + queue.append(node.left) + if node.right: + queue.append(node.right) + prev = node + return root + ``` + + + + + ```cpp + class Solution { +public: + Node* connect(Node* root) { + if (!root) return nullptr; + queue q; + q.push(root); + while (!q.empty()) { + int levelSize = q.size(); + Node* prev = nullptr; + for (int i = 0; i < levelSize; ++i) { + Node* node = q.front(); + q.pop(); + if (prev) { + prev->next = node; + } + if (node->left) q.push(node->left); + if (node->right) q.push(node->right); + prev = node; + } + } + return root; + } +}; + ``` + + + + +#### Complexity Analysis + +- Time Complexity: $O(n)$ where n is the number of nodes in the binary tree. +- Space Complexity: $O(m)$ where m is the maximum number of nodes at any level in the binary tree. In the worst case, the queue can contain all nodes at the last level, which is at most $2^{h-1}$ where h is the height of the tree. + + + + + +---