| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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
- 영어원서읽기
- 화상영어
- 영어공부
- 읽기
- 월간
- 리얼 클래스
- 링피트
- 쓰릴오브파이트
- 스탭퍼
- 미드시청
- 파비최
- leetcode
- 개발자
- Writing
- 뭐든
- 괜찮음
- 3줄정리
- Daily Challenge
- 30분
- 만화도
- 잡생각
- 프로젝트
- English
- 운동
- realclass
- Problem Solving
- 매일
- FIT XR
- 사이드
- 10분
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