| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- 알고리즘
- 파이썬
- Greedy
- codeup
- heap
- 코드업
- nlp
- two-pointer
- binary Tree
- 릿코드
- SQL
- 리트코드
- 니트코드
- 생성형AI
- 파이썬알고리즘
- GenerativeAI
- Stack
- Python3
- LeetCode
- 슬라이딩윈도우
- BFS
- Python
- graph
- array
- stratascratch
- GenAI
- 투포인터
- dfs
- sql코테
- tree
Archives
- Today
- Total
Tech for good
[Leetcode/Tree, DFS, Binary Tree, Recursion] 112. Path Sum 본문
IT/Computer Science
[Leetcode/Tree, DFS, Binary Tree, Recursion] 112. Path Sum
Diana Kang 2025. 7. 23. 04:50

| Binary Tree | Binary Search Tree |
|
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
if root is None:
return False
if not root.left and not root.right:
return targetSum == root.val
right = self.hasPathSum(root.right, targetSum-root.val)
left = self.hasPathSum(root.left, targetSum-root.val)
return left or right