Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 파이썬 릿코드
- 알고리즘풀이
- 릿코드 파이썬
- python priority queue
- 릿코드풀이
- 파이썬릿코드풀기
- python zip_longest
- leetcode풀이
- 파이썬 알고리즘 풀기
- python xor
- 릿코드 풀기
- 파이썬릿코드
- binary search
- leetcode풀기
- 파이썬 알고리즘
- 코틀린기초
- 파이썬 프로그래머스
- 상가수익률계산기
- 알고리즘풀기
- python 릿코드
- python 알고리즘
- 파이썬알고리즘풀기
- 릿코드풀기
- 잇츠디모
- 릿코드
- 파이썬알고리즘
- python sorted
- python Leetcode
- leetcode 풀기
- LeetCode
Archives
- Today
- Total
소프트웨어에 대한 모든 것
LeetCode 풀기 - 662. Maximum Width of Binary Tree 본문
반응형
662. Maximum Width of Binary Tree
https://leetcode.com/problems/maximum-width-of-binary-tree/
Maximum Width of 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
문제)
솔루션1) 재귀
문제에 대한 이해를 확실히 하지 못해서 많이 헤맸습니다.
문제가 원하는 바를 다시 정리하자면,
"바이너리 트리에서 모든 레벨에서의 최대 폭을 리턴하는 것'입니다.
문제를 해결하기 위해서는,
1) 각 레벨에서의 폭을 계산한다.
2) 모든 레벨을 순회하면서 최대 폭을 찾는다.
바이너리 트리의 depth별 폭을 dict 형태로 계산해서 최대 폭을 계산하는 방법을 취합니다.
# 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 widthOfBinaryTree(self, root: Optional[TreeNode]) -> int:
# key = depth, value=[column numbers]
d = defaultdict(list)
def recur(node, depth, column):
if node is None: return
d[depth].append(column)
recur(node.left, depth+1, column*2)
recur(node.right, depth+1, column*2+1)
recur(root, depth=0, column=0)
return max([(max(d[depth]) - min(d[depth]) + 1) for depth in d])
함께 보면 좋은 글:
반응형
'알고리즘 > LeetCode' 카테고리의 다른 글
LeetCode 풀기 - 64. Minimum Path Sum (0) | 2022.02.28 |
---|---|
LeetCode 풀기 - 108. Convert Sorted Array to Binary Search Tree (0) | 2022.02.27 |
LeetCode 풀기 - 49. Group Anagrams (0) | 2022.02.26 |
LeetCode 풀기 - 2169. Count Operations to Obtain Zero (0) | 2022.02.26 |
LeetCode 풀기 - 347. Top K Frequent Elements (0) | 2022.02.25 |
Comments