
Sponsored
Sponsored
Using a recursive DFS, traverse each path from root to leaf, maintaining the current path and its corresponding sum. When a leaf is reached, check if the path's sum matches the targetSum. Use backtracking to explore all paths.
Time Complexity: O(N), where N is the number of nodes in the tree.
Space Complexity: O(H), where H is the height of the tree, due to the recursion stack.
1#include <stdio.h>
2#include <stdlib.h>
3
4struct TreeNode {
5 int val;
6 struct TreeNode *left;
7 struct TreeNode *right;
8};
9
10void dfs(struct TreeNode* root, int targetSum, int currentSum, int* path, int pathIndex, int** result, int* returnSize, int** columnSizes) {
11 if (!root) return;
12 currentSum += root->val;
13 path[pathIndex++] = root->val;
14 if (!root->left && !root->right && currentSum == targetSum) {
15 result[*returnSize] = (int*)malloc(pathIndex * sizeof(int));
16 columnSizes[0][*returnSize] = pathIndex;
17 for (int i = 0; i < pathIndex; ++i) {
18 result[*returnSize][i] = path[i];
19 }
20 (*returnSize)++;
21 } else {
22 dfs(root->left, targetSum, currentSum, path, pathIndex, result, returnSize, columnSizes);
23 dfs(root->right, targetSum, currentSum, path, pathIndex, result, returnSize, columnSizes);
24 }
25}
26
27int** pathSum(struct TreeNode* root, int targetSum, int* returnSize, int** columnSizes) {
28 *returnSize = 0;
29 int** result = (int**)malloc(1000 * sizeof(int*));
30 *columnSizes = (int*)malloc(1000 * sizeof(int));
31 int* path = (int*)malloc(1000 * sizeof(int));
32 dfs(root, targetSum, 0, path, 0, result, returnSize, columnSizes);
33 free(path);
34 return result;
35}We use a helper function, dfs, to recursively explore paths. We store current paths and their sums in arrays to keep track of potential solutions. When a solution is found at a leaf node, it is added to the result set, and backtracking ensures all paths are checked.
An iterative approach using a stack can also be used for this problem. We maintain stacks that parallel traditional DFS traversal with added structures to track path and cumulative sum states for efficient exploration of all feasible root-to-leaf paths.
Time Complexity: O(N), as each node is processed exactly once.
Space Complexity: O(N), since results and stack can store all nodes during worst-case full-tree traversal.
1#include <vector>
2#include <stack>
3using namespace std;
4
5struct TreeNode {
6 int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
class Solution {
public:
vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
vector<vector<int>> result;
if (!root) return result;
stack<pair<TreeNode*, pair<int, vector<int>>>> stk;
stk.push({root, {root->val, {root->val}}});
while (!stk.empty()) {
auto [node, state] = stk.top(); stk.pop();
auto [currentSum, path] = state;
if (!node->left && !node->right && currentSum == targetSum) {
result.push_back(path);
}
if (node->right) {
path.push_back(node->right->val);
stk.push({node->right, {currentSum + node->right->val, path}});
path.pop_back();
}
if (node->left) {
path.push_back(node->left->val);
stk.push({node->left, {currentSum + node->left->val, path}});
path.pop_back();
}
}
return result;
}
};This C++ solution employs a stack to simulate DFS traversal iteratively. The stack holds node, cumulative sum, and path data. As each node is processed, we check for valid paths and update the result list accordingly. Paths are extended by exploring left and right children, augmented by the current node value.