일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- Problem Solving
- 매일
- 개발자
- 잡생각
- English
- 미드시청
- 30분
- 운동
- leetcode
- Writing
- 영어원서읽기
- 프로젝트
- 월간
- realclass
- 괜찮음
- 영어공부
- FIT XR
- Daily Challenge
- 리얼 클래스
- 뭐든
- 화상영어
- 쓰릴오브파이트
- 읽기
- 스탭퍼
- 링피트
- 10분
- 파비최
- 만화도
- 사이드
- 3줄정리
Archives
- Today
- Total
파비의 매일매일 공부기록
2023.11.02 Today's Challenge 본문
https://leetcode.com/problems/find-mode-in-binary-search-tree/
Find Mode in Binary Search Tree - LeetCode
Can you solve this real interview question? Find Mode in Binary Search Tree - Given the root of a binary search tree (BST) with duplicates, return all the mode(s) [https://en.wikipedia.org/wiki/Mode_(statistics)] (i.e., the most frequently occurred element
leetcode.com
DFS로 푸는 문제.
DFS에도 종류가 좀 있구만..
def findMode(self, root: Optional[TreeNode]) -> List[int]:
def dfs(node, counter):
if not node:
return
counter[node.val] += 1
dfs(node.left, counter)
dfs(node.right, counter)
counter = defaultdict(int)
dfs(root, counter)
max_freq = max(counter.values())
ans = []
for key in counter:
if counter[key] == max_freq:
ans.append(key)
return ans
반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
2023.11.04 Today's Challenge (0) | 2023.11.04 |
---|---|
2023.11.03 Today's Challenge (0) | 2023.11.03 |
2023.11.01 Today's Challenge (0) | 2023.11.01 |
2023.10.31 Today's Challenge (0) | 2023.10.31 |
2023.10.30 Today's Challenge (0) | 2023.10.30 |
Comments