Repository navigation
[freemjstudio] WEEK 14 Solutions #2872
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| from collections import deque | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 복잡도 분석을 주석으로 써주시면 좋을 것 같아요 |
||
|
|
||
| # 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 levelOrder(self, root: TreeNode | None) -> list[list[int]]: | ||
| visits = [] | ||
|
|
||
| # bfs | ||
| queue = deque([]) | ||
| queue.append(root) | ||
|
|
||
| while queue: | ||
| temp = [] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. temp 보다 조금 더 명시적으로 드러나는 변수명을 써주시면 좋을 것 같습니다~ |
||
| for _ in range(len(queue)): | ||
| node = queue.popleft() | ||
| if node: | ||
| temp.append(node.val) | ||
|
|
||
| if node and node.left: | ||
| queue.append(node.left) | ||
| if node and node.right: | ||
| queue.append(node.right) | ||
|
Comment on lines
+21
to
+27
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 뒤의 두 if 문을 맨 앞 조건 문 안으로 넣어주면 불필요한 if node 체크 로직을 줄일 수 있어 보이네요. |
||
| if temp: | ||
| visits.append(temp) | ||
|
|
||
| return visits | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🏷️ 알고리즘 패턴 분석
binary-tree-level-order-traversal/freemjstudio.py