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
56 changes: 56 additions & 0 deletions binary-tree-level-order-traversal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#----------------Solution 1 : BFS level traversal-----------
''' Time Complexity : O(n)
Space Complexity : O(n)
Did this code successfully run on Leetcode : Yes
Any problem you faced while coding this : No

Approach : Maintain the size of queue. After traversing one level add the list to result
'''
class Solution:
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
if root is None:
return []
result = []
q = []
q.append(root)
while q:
size = len(q)
l = []
for i in range(size):
node = q.pop(0)
l.append(node.val)
if node.left:
q.append(node.left)
if node.right:
q.append(node.right)
result.append(l)

return result

#----------------Solution 1 : DFS traversal-----------
''' Time Complexity : O(n)
Space Complexity : O(h)
Did this code successfully run on Leetcode : Yes
Any problem you faced while coding this : No

Approach : Maintain global result, when new level is encountered create new list at the index of level.
Traverse the tree in preorder and append the nodes at respective index list.
'''
class Solution:
def __init__(self):
self.result = []

def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:

def dfs(root,level):
if root is None:
return
if len(self.result) == level:
self.result.append([])

self.result[level].append(root.val)
dfs(root.left,level+1)
dfs(root.right,level+1)

dfs(root, 0)
return self.result
69 changes: 69 additions & 0 deletions course-schedule.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#----------------Solution 1 : BFS -----------
''' Time Complexity : O(V + E)
Space Complexity : O(V + E)
Did this code successfully run on Leetcode : Yes
Any problem you faced while coding this : No

Approach : Using topological sort algorithm
'''
class Solution:
def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
hashmap = defaultdict(list)
count = 0
indegree = [0 for _ in range(numCourses)]
for crs,pre in prerequisites:
hashmap[pre].append(crs)
indegree[crs] += 1
q = []
for i in range(numCourses):
if indegree[i] == 0:
q.append(i)
count += 1
while q:
crs = q.pop(0)
for pre in hashmap[crs]:
indegree[pre] -= 1
if indegree[pre] == 0:
q.append(pre)
count += 1

if count == numCourses:
return True
else:
return False

#----------------Solution 2 : DFS -----------
''' Time Complexity : O(V + E)
Space Complexity : O(V + E)
Did this code successfully run on Leetcode : Yes
Any problem you faced while coding this : No

Approach : Maintaining visited set to detect cycle, Travresing each node by dfs
'''
class Solution:
def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
hashmap = {i:[] for i in range(numCourses)}
for crs,pre in prerequisites:
hashmap[pre].append(crs)

visited = set()
def dfs(crs):
#if no prerequisite return True
if hashmap[crs] == []:
return True
if crs in visited:
return False
visited.add(crs)
for pre in hashmap[crs]:
if not dfs(pre):
return False
#if True for all prereq remove from visited
visited.remove(crs)
# Dont traverse completed crs again
hashmap[crs] = []
return True
#call dfs for all courses
for i in range(numCourses):
if not dfs(i):
return False
return True