Skip to content

Commit cc0f81d

Browse files
committed
[:solved] 2 problems
1 parent dc29245 commit cc0f81d

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
8+
9+
# idea: BFS
10+
# Time Complexity: O(n)
11+
from collections import deque
12+
class Solution:
13+
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
14+
ans = []
15+
q = deque([root])
16+
while q:
17+
level = []
18+
for i in range(len(q)):
19+
node = q.popleft()
20+
if node:
21+
level.append(node.val)
22+
if node.left:
23+
q.append(node.left)
24+
if node.right:
25+
q.append(node.right)
26+
if level:
27+
ans.append(level)
28+
return ans
29+
30+

counting-bits/ppxyn1.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# idea : -
2+
# Time Complexity : O(n log n) since counting?
3+
class Solution:
4+
def countBits(self, n: int) -> List[int]:
5+
ans = []
6+
for i in range(n + 1):
7+
ans.append(bin(i)[2:].count("1"))
8+
return ans
9+
10+
# TODO: O(n)?
11+
12+
13+

0 commit comments

Comments
 (0)