Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions binary-tree-level-order-traversal/ppxyn1.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

level을 따로 측정하셨군요! 같은 레벨의 노드는 while 내부의 for문이 도는 과정에서 모두 pop되니, 좋은 방법이라고 생각됩니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# 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


# idea: BFS
# Time Complexity: O(n)
from collections import deque
class Solution:
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
ans = []
q = deque([root])
while q:
level = []
for i in range(len(q)):
node = q.popleft()
if node:
level.append(node.val)
if node.left:
q.append(node.left)
if node.right:
q.append(node.right)
if level:
ans.append(level)
return ans


13 changes: 13 additions & 0 deletions counting-bits/ppxyn1.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

최적화 방법이 있다고는 하지만, 이 풀이처럼 역시 간단한 해결책으로 먼저 수행하고 생각해 보는 것이 좋다고 생각합니다. 코딩 테스트와 코딩 인터뷰는 사뭇 다르겠습니다만, 어쨌든 일단 맞춰야 그 다음이 있겠죠. 개인적으로 저도 익숙지 않은 언어로 일부러 풀어 보곤 하는데, 그러다 보니 똑같은 풀이로 풀었습니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# idea : -
# Time Complexity : O(n log n) since counting?
class Solution:
def countBits(self, n: int) -> List[int]:
ans = []
for i in range(n + 1):
ans.append(bin(i)[2:].count("1"))
return ans

# TODO: O(n)?