one line of code at a time
[leetcode] 113. Path Sum II 파이썬 코드 본문
https://leetcode.com/problems/path-sum-ii/description/
class Solution(object):
def pathSum(self, root, targetSum):
result = []
if not root: return result
def dfs(root, curr_sum, curr_list):
curr_sum += root.val
temp = curr_list + [root.val]
if root.left:
dfs(root.left, curr_sum, temp)
if root.right:
dfs(root.right, curr_sum, temp)
if not root.left and not root.right and curr_sum == targetSum:
# leaf node까지 왔을 때 targetSum과 같은지 체크
result.append(temp)
temp = []
dfs(root, 0, temp)
return result'leetcode' 카테고리의 다른 글
| [leetcode] 876. Middle of the Linked List (0) | 2024.08.22 |
|---|---|
| [leetcode] 547. Number of Provinces 파이썬 코드 (0) | 2024.08.18 |
| [leetcode] 112. Path Sum 파이썬 코드 (0) | 2024.08.17 |
| [leetcode] 1448. Count Good Nodes in Binary Tree 파이썬 코드 (0) | 2024.08.16 |
| [leetcode] 872. Leaf-Similar Trees 파이썬 코드 (0) | 2024.08.16 |