일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 슬라이딩윈도우
- GenAI
- 투포인터
- slidingwindow
- Stack
- Greedy
- Python
- nlp
- 파이썬알고리즘
- heap
- 니트코드
- gcp
- dfs
- 릿코드
- 코드업
- 자연어처리
- 생성형AI
- GenerativeAI
- sql코테
- Python3
- LeetCode
- array
- two-pointer
- stratascratch
- 리트코드
- codeup
- 파이썬기초100제
- 알고리즘
- 파이썬
- SQL
Archives
- Today
- Total
Tech for good
[Leetcode/Tree] 404. Sum of Left Leaves 본문
# 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 sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
total = 0
# 왼쪽 자식이 리프 노드라면 값 추가
if root.left and not root.left.left and not root.left.right:
total += root.left.val
# 왼쪽과 오른쪽 서브트리 탐색 (재귀 호출)
total += self.sumOfLeftLeaves(root.left)
total += self.sumOfLeftLeaves(root.right)
return total