Skip to content
Open

'bfs-1' #1602

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
52 changes: 52 additions & 0 deletions course-schedule.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Initialize a queue q and an indegree list with values 0 for numCourses no. of elements.
# Assign empty list for adjacency list for 0 to numCourses value. Loop through prerequisites i
# and increment indegree[i] by 1 and also append the element at [0] to the adj matrix.
# Loop through indegree and if the element is 0 add the index to the queue q. WHile q is not empty
# pop the vlaue from the left and check its key on adj matric and the indegree indexes with its's value will be
# decremented. If any value reached 0, that index is added to the queue
# Lastly, looop through indegree array, if any value is not 0, then return false else return true

# Time: O(V+E)
# Space:O(V+E)

class Solution(object):
def canFinish(self, numCourses, prerequisites):
"""
:type numCourses: int
:type prerequisites: List[List[int]]
:rtype: bool
"""
q=deque()
indegree=[0 for i in range(numCourses)]
adj_m={}
for i in range(numCourses):
adj_m[i]=[]
# print("adj", adj_m)
for i in prerequisites:
indegree[i[0]] =indegree[i[0]]+1

adj_m[i[1]].append(i[0])

# print("ind",indegree)
# print("adj_m",adj_m)
for ind, i in enumerate(indegree):
if i ==0:
q.append(ind)
# print('queue===',q)
if not q:
return False
while q:
p=q.popleft()
# print('ad',adj_m, p, q)
x=adj_m[p]
for i in x:

indegree[i]=indegree[i]-1
if indegree[i]==0:
q.append(i)
# print(q,'k', indegree)
for i in indegree:
if i!=0:
return False

return True
94 changes: 94 additions & 0 deletions level-order-traversal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# Initialize a queue and append the initial root to it. While the queue is not empty,
# find the size of the queue and initialize list l with None of size of the queue.
# Until the size of the queue, run a loop where the elemetns are popped from the queue and added
# into the last avaialble spot of l and then append the left and right of the element to
# the queue. Append the list l to the result.

# Time complexity: O(n)
# Space complexity:O(n)



# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution(object):
def levelOrder(self, root):
"""
:type root: Optional[TreeNode]
:rtype: List[List[int]]

"""

q=deque()
if root==None:
return []
q.append(root)
result=[]
l=[None]

while q:
# print("-----",q,"++")
size=len(q)
l=[None for i in range(size)]
for i in range(size):
q_pop=q.popleft()

l[i]=q_pop.val
# print(q_pop)
if q_pop.left:
q.append(q_pop.left)
if q_pop.right:
q.append(q_pop.right)
result.append(l)

return result



# Using DFS
# Initialize global variable for level and list. In recursion, check if root is null,
# if so return. Else check if len(l)<level, if it is , append list l with a new list
# containing the the root.val else add the root value at the end of l[level]

Time complexity: O(n)
Space conplexity: O(n)

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution(object):
def levelOrder(self, root):
"""
:type root: Optional[TreeNode]
:rtype: List[List[int]]
"""
level=0
l=[]
self.helper(root,level,l)
return l


def helper(self, root, level, l):
if root==None:
return

if len(l)-1<level:
l.append([root.val])
else:
l[level].append(root.val)



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



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