-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathPath Sum II.cpp
71 lines (59 loc) · 3.08 KB
/
Path Sum II.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* Scroll down to see JAVA code also */
/*
MY YOUTUBE VIDEO ON THIS Qn : https://www.youtube.com/watch?v=eoyO8hOkPNs&t=3s
Company Tags : Bloomberg, LinkedIn, Amazon, Bloomberg, Quora
Leetcode Link : https://leetcode.com/problems/path-sum-ii/
*/
/************************************************************ C++ ************************************************************/
//T.C : The time complexity of the code is O(N), where N is the number of nodes in the binary tree.
//This is because each node is visited exactly once during the recursive traversal.
//Note that I am ignoring the time taken to move the temp values to result - result.push_back(temp);
//S.C : The space complexity is O(H) in the worst case, where H is the height of the binary tree.
//This is due to the recursion stack during the depth-first search. In the worst case,
//the recursion stack will have H function calls, where H is the height of the tree.
class Solution {
public:
void collectPaths(TreeNode* root, int curr, vector<int>& temp, vector<vector<int>>& result) {
if(!root)
return;
temp.push_back(root->val);
if(root->left == NULL && root->right == NULL && root->val == curr) {
result.push_back(temp);
}
collectPaths(root->left, curr-root->val, temp, result);
collectPaths(root->right, curr-root->val, temp, result);
temp.pop_back();
}
vector<vector<int>> pathSum(TreeNode* root, int sum) {
vector<vector<int>> result;
vector<int> temp;
collectPaths(root, sum, temp, result);
return result;
}
};
/************************************************************ JAVA ************************************************************/
//T.C : The time complexity of the code is O(N), where N is the number of nodes in the binary tree.
//This is because each node is visited exactly once during the recursive traversal.
//Note that I am ignoring the time taken to move the temp values to result - result.push_back(temp);
//S.C : The space complexity is O(H) in the worst case, where H is the height of the binary tree.
//This is due to the recursion stack during the depth-first search. In the worst case,
//the recursion stack will have H function calls, where H is the height of the tree.
public class Solution {
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> result = new ArrayList<>();
List<Integer> temp = new ArrayList<>();
collectPaths(root, sum, temp, result);
return result;
}
private void collectPaths(TreeNode root, int curr, List<Integer> temp, List<List<Integer>> result) {
if (root == null)
return;
temp.add(root.val);
if (root.left == null && root.right == null && root.val == curr) {
result.add(new ArrayList<>(temp));
}
collectPaths(root.left, curr - root.val, temp, result);
collectPaths(root.right, curr - root.val, temp, result);
temp.remove(temp.size() - 1);
}
}