일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 링피트
- 괜찮음
- 30분
- 3줄정리
- 개발자
- 월간
- 쓰릴오브파이트
- 운동
- 만화도
- 리얼 클래스
- 사이드
- 화상영어
- Writing
- 영어원서읽기
- 프로젝트
- 미드시청
- 매일
- 파비최
- 10분
- FIT XR
- realclass
- English
- 스탭퍼
- Problem Solving
- 잡생각
- Daily Challenge
- 뭐든
- 영어공부
- 읽기
- leetcode
Archives
- Today
- Total
파비의 매일매일 공부기록
2023.11.03 Today's Challenge 본문
https://leetcode.com/problems/count-nodes-equal-to-average-of-subtree/
Count Nodes Equal to Average of Subtree - LeetCode
Can you solve this real interview question? Count Nodes Equal to Average of Subtree - Given the root of a binary tree, return the number of nodes where the value of the node is equal to the average of the values in its subtree. Note: * The average of n ele
leetcode.com
DFS로 푸는 문제.
몇일째 트리문제만 나오는중 ㅋㅋ
class Solution:
def __init__(self):
self.msc = 0
def cal(self, cnode):
if cnode is None:
return 0, 0
ls = self.cal(cnode.left)
rs = self.cal(cnode.right)
sv = ls[0] + rs[0] + cnode.val
nn = ls[1] + rs[1] + 1
if sv // nn == cnode.val:
self.msc += 1
return sv, nn
def averageOfSubtree(self, root: Optional[TreeNode]) -> int:
self.cal(root)
return self.msc
반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
2023.11.05 Today's Challenge (0) | 2023.11.05 |
---|---|
2023.11.04 Today's Challenge (0) | 2023.11.04 |
2023.11.02 Today's Challenge (0) | 2023.11.02 |
2023.11.01 Today's Challenge (0) | 2023.11.01 |
2023.10.31 Today's Challenge (0) | 2023.10.31 |
Comments