일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 31 |
Tags
- 10분
- 30분
- 개발자
- 읽기
- Problem Solving
- 운동
- realclass
- 영어원서읽기
- English
- Writing
- 링피트
- 스탭퍼
- leetcode
- 리얼 클래스
- 쓰릴오브파이트
- 3줄정리
- 파비최
- FIT XR
- 사이드
- 뭐든
- 화상영어
- 미드시청
- 프로젝트
- 매일
- 만화도
- 월간
- 잡생각
- 영어공부
- 괜찮음
- Daily Challenge
Archives
- Today
- Total
파비의 매일매일 공부기록
Today's Challenge 본문
https://leetcode.com/problems/average-of-levels-in-binary-tree/
Average of Levels in Binary Tree - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
Queue를 이용한 BFS로 간단히 풀 수 있음.
class Solution:
def averageOfLevels(self, root: Optional[TreeNode]) -> List[float]:
qu = []
res = []
qu.append(root)
while qu:
l = len(qu)
temp = 0
for i in range(l):
node = qu.pop(0)
temp += node.val
if node.left:
qu.append(node.left)
if node.right:
qu.append(node.right)
res.append(temp/l)
return res
반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
Today's Challenge (0) | 2022.09.04 |
---|---|
Today's Challenge (0) | 2022.09.03 |
Today's Challenge (0) | 2022.09.01 |
Today's Challenge (0) | 2022.08.31 |
Today's Challenge (0) | 2022.08.30 |
Comments