Skip to content

Commit 05a8d5e

Browse files
committed
day 4
1 parent 7373b88 commit 05a8d5e

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
Path Sum II
3+
===========
4+
5+
Given the root of a binary tree and an integer targetSum, return all root-to-leaf paths where each path's sum equals targetSum.
6+
7+
A leaf is a node with no children.
8+
9+
Example 1:
10+
Input: root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
11+
Output: [[5,4,11,2],[5,8,4,5]]
12+
13+
Example 2:
14+
Input: root = [1,2,3], targetSum = 5
15+
Output: []
16+
17+
Example 3:
18+
Input: root = [1,2], targetSum = 0
19+
Output: []
20+
21+
Constraints:
22+
The number of nodes in the tree is in the range [0, 5000].
23+
-1000 <= Node.val <= 1000
24+
-1000 <= targetSum <= 1000
25+
*/
26+
27+
/**
28+
* Definition for a binary tree node.
29+
* struct TreeNode {
30+
* int val;
31+
* TreeNode *left;
32+
* TreeNode *right;
33+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
34+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
35+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
36+
* };
37+
*/
38+
39+
class Solution
40+
{
41+
public:
42+
void dfs(TreeNode *root, vector<vector<int>> &ans, vector<int> &path, int target, int curr)
43+
{
44+
if (!root->left && !root->right)
45+
{
46+
path.push_back(root->val);
47+
if (curr + root->val == target)
48+
ans.push_back(path);
49+
path.pop_back();
50+
return;
51+
}
52+
53+
path.push_back(root->val);
54+
if (root->left)
55+
dfs(root->left, ans, path, target, curr + root->val);
56+
if (root->right)
57+
dfs(root->right, ans, path, target, curr + root->val);
58+
path.pop_back();
59+
}
60+
61+
vector<vector<int>> pathSum(TreeNode *root, int targetSum)
62+
{
63+
if (!root)
64+
return {};
65+
66+
vector<vector<int>> ans;
67+
vector<int> curr;
68+
dfs(root, ans, curr, targetSum, 0);
69+
return ans;
70+
}
71+
};

Leetcode Daily Challenge/August-2021/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
| :-: | :------------------------------------------------------------------------------------------------------------------------------------------- | :--------------------------------------------: |
55
| 1. | [Making A Large Island](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3835/) | [cpp](./01.%20Making%20A%20Large%20Island.cpp) |
66
| 2. | [Two Sum](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3836/) | [cpp](./02.%20Two%20Sum.cpp) |
7-
| 3. | [Subsets I](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3837/) | [cpp](./03.%20Subsets%20II.cpp) |
7+
| 3. | [Subsets II](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3837/) | [cpp](./03.%20Subsets%20II.cpp) |
8+
| 4. | [Path Sum II](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3838/) | [cpp](./04.%20Path%20Sum%20II.cpp) |

0 commit comments

Comments
 (0)