From beccb8466941911402436e0c6fefb885f36e0cd5 Mon Sep 17 00:00:00 2001 From: Yunyoung Chung Date: Sat, 6 Jun 2026 00:00:34 +0900 Subject: [PATCH 1/2] [7th batch] week - binary tree level order traversal --- binary-tree-level-order-traversal/liza0525.py | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 binary-tree-level-order-traversal/liza0525.py diff --git a/binary-tree-level-order-traversal/liza0525.py b/binary-tree-level-order-traversal/liza0525.py new file mode 100644 index 0000000000..350a6c6e46 --- /dev/null +++ b/binary-tree-level-order-traversal/liza0525.py @@ -0,0 +1,56 @@ +# 7기 풀이 +class Solution: + # 풀이 1 - BFS + # 시간 복잡도: O(n) + # - 노드의 개수(n)만큼 모두 탐색하므로 + # 공간 복잡도: O(w) + # - 최악은 한 레벨의 최대 노드 수(w)만큼 queue에 쌓임 + def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: + res = [] + + nodes = deque() + if root: + nodes.appendleft(root) + + while nodes: + childs = deque() + sibling_vals = [] + while nodes: + node = nodes.pop() + if not node: + continue + + sibling_vals.append(node.val) + if node.left: + childs.appendleft(node.left) + if node.right: + childs.appendleft(node.right) + + res.append(sibling_vals) + nodes = childs + + return res + + # 풀이 2 - DFS + # 시간 복잡도: O(n) + # - 노드의 개수(n)만큼 모두 탐색하므로 + # 공간 복잡도: O(h) + # - 재귀 스택이 최대 나무의 높이(h)만큼 쌓임 + def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: + res = [] + + def dfs(node, depth): + if not node: + return + + if len(res) == depth: + res.append([node.val]) + else: + res[depth].append(node.val) + + dfs(node.left, depth + 1) + dfs(node.right, depth + 1) + + dfs(root, 0) + + return res From f4e8e3f2474cadfb2bffd5d1de83e0a17e5da6e7 Mon Sep 17 00:00:00 2001 From: Yunyoung Chung Date: Sat, 6 Jun 2026 00:00:51 +0900 Subject: [PATCH 2/2] [7th batch] week 14 - counting bits --- counting-bits/liza0525.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 counting-bits/liza0525.py diff --git a/counting-bits/liza0525.py b/counting-bits/liza0525.py new file mode 100644 index 0000000000..7e75f43b75 --- /dev/null +++ b/counting-bits/liza0525.py @@ -0,0 +1,16 @@ +# 7기 풀이 +# 시간 복잡도: O(n) +# - n의 크기만큼 모든 수에 대한 dp 값을 찾음 +# 공간 복잡도: O(n) +# - n의 크기만큼 dp 어레이를 사용 +class Solution: + def countBits(self, n: int) -> List[int]: + dp = [0 for _ in range(n + 1)] + + for i in range(1, n + 1): + if i % 2 == 1: + dp[i] = dp[i - 1] + 1 + else: + dp[i] = dp[i // 2] + + return dp