Skip to content
Open

BFS-1 #1650

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
63 changes: 63 additions & 0 deletions Problem1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# 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

# Time Complexity --> O(n) where n is the number of nodes
# Space Complexity --> O(n/2)
# Approach --> BFS
from collections import deque
class Solution:
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
if root is None:
return []
q = deque()
q.append(root)
result = []
while q:
size = len(q)
temp = []
for i in range(size):
curr = q.popleft()
temp.append(curr.val)
if curr.left is not None:
q.append(curr.left)
if curr.right is not None:
q.append(curr.right)
result.append(temp)
return result

'''
# 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

# Time Complexity --> O(n) where n is the number of nodes
# Space Complexity --> O(logn) which is the recursive stack size(height of the tree)
# Approach --> DFS
class Solution:
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
self.result = []
self.helper(root, 0)
return self.result

def helper(self, root, level):
# base
if root is None:
return

# logic
if len(self.result)==level:
self.result.append([])
self.result[level].append(root.val)

self.helper(root.left, level+1)
self.helper(root.right, level+1)


'''
39 changes: 39 additions & 0 deletions Problem2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Time Complexity --> O(V + E) where v is the number of vertices or courses and E is the number of edges or dependencies
# Space Complexity --> O(V + E)
class Solution:
def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:

hmap = {}
indegree = [0 for i in range(numCourses)]
for prereq in prerequisites:
if prereq[1] not in hmap:
hmap[prereq[1]] = []
hmap[prereq[1]].append(prereq[0]) # Independent(Prereq) to Dependent mapping
indegree[prereq[0]] += 1 # keep track of indegree

q = deque()
count = 0
for i in range(len(indegree)):
if indegree[i]==0:
q.append(i)
count += 1

if count==numCourses: # If there are no courses with prereqs then return true
return True
if len(q)==0: # If there are no courses with 0 prereqs, then it is a cyclic graph and return False
return False

# Start with the independent courses and then look over all their dependent courses, reduce each of indegree by 1 and if all the indegrees get to 0 then return True
while q:
curr = q.popleft()
if curr in hmap and len(hmap[curr])>0:
for course in hmap[curr]:
indegree[course] -= 1
if indegree[course]==0:
q.append(course)
count += 1
if count==numCourses:
return True
return False