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
- array
- python3
- 광연자동차운전면허학원
- Medium
- DailyLeetCoding
- Java
- VCS
- ArrayList vs LinkedList
- String
- dfs
- greedy
- heap
- 자료구조
- 구현
- SinglyLinkedList
- Two Pointers
- Easy
- hash
- Leedcode
- sorting
- Union Find
- LinkedList
- BFS
- leetcode
- hash table
- stack
- Bellman-Ford
- Hashtable
- graph
- A* Algorithm
Archives
- Today
- Total
Min IT's Devlog
[python3] 429. N-ary Tree Level Order Traversal 본문
풀이 일자: 22.09.05
난이도: [Medium]
분류: [BFS/ Tree]

문제 내용
주어진 n-ary tree를 가지고 각 level의 노드값들의 리스트들을 하나로 묶어서 리턴하는 문제이다.
문제 해결 흐름
1. 문제에서 level별 순회값을 리턴하라고 어떻게 풀어야 하는지 제시해주었다.
→ 대놓고 BFS쓰라는 거구나. 그래도 일단 익숙한 DFS로 풀어보았다.
"""
# Definition for a Node.
class Node:
def __init__(self, val=None, children=None):
self.val = val
self.children = children
"""
class Solution:
def levelOrder(self, root: 'Node') -> List[List[int]]:
ans = list()
dq = deque()
dq.append((root,0))
while dq:
node, level = dq.popleft()
if node:
if len(ans) <= level:
ans.append([])
ans[level].append(node.val)
for child in node.children:
dq.append((child, level + 1))
return ans
2. 문제에서 요청한 대로 BFS로 풀어본다면...
다른 해결 방식
문제 링크
https://leetcode.com/problems/n-ary-tree-level-order-traversal/
'코테 > LeetCode(Solve)' 카테고리의 다른 글
| [python3] 1328. Break a Palindrome (0) | 2022.10.11 |
|---|---|
| [python3] 1996. The Number of Weak Characters in the Game (0) | 2022.09.09 |
| [python3] 987. Vertical Order Traversal of a Binary Tree (0) | 2022.09.04 |
| [python3] 967. Numbers With Same Consecutive Differences (0) | 2022.09.03 |
| [python3] 637. Average of Levels in Binary Tree (0) | 2022.09.03 |
Comments