Skip to content

Commit 8589875

Browse files
committed
problem: 0113 path sum
1 parent 61ce91d commit 8589875

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

0113 Path Sum II/0113 Path Sum II.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# 0113. Path Sum II
2+
3+
4+
* Difficulty: medium
5+
* Link: https://leetcode.com/problems/path-sum-ii/
6+
* Topics: DFS-BFS
7+
8+
# Clarification
9+
10+
1. Check the inputs and outputs
11+
12+
# Solution
13+
14+
### Thought Process
15+
16+
1. traverse all path by DFS and compare the sum
17+
- Implement
18+
19+
```python
20+
# Definition for a binary tree node.
21+
# class TreeNode:
22+
# def __init__(self, val=0, left=None, right=None):
23+
# self.val = val
24+
# self.left = left
25+
# self.right = right
26+
class Solution:
27+
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
28+
result = []
29+
if not root:
30+
return result
31+
32+
def dfs(current, path, res):
33+
path.append(current.val)
34+
res -= current.val
35+
if not current.left and not current.right:
36+
if res == 0:
37+
return result.append(path[:])
38+
if current.left:
39+
dfs(current.left, path[:], res)
40+
if current.right:
41+
dfs(current.right, path[:], res)
42+
43+
dfs(root, [], targetSum)
44+
return result
45+
```
46+
47+
48+
### Complexity
49+
50+
- Time complexity:
51+
- Space complexity:

0 commit comments

Comments
 (0)