From da00498dee39e31866b56f0ffd43b3443645c854 Mon Sep 17 00:00:00 2001 From: Vaishnavi Gawale Date: Thu, 22 Jan 2026 13:12:58 -0500 Subject: [PATCH] BFS-1 Done --- binary-tree-level-order-traversal.py | 56 ++++++++++++++++++++++ course-schedule.py | 69 ++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 binary-tree-level-order-traversal.py create mode 100644 course-schedule.py diff --git a/binary-tree-level-order-traversal.py b/binary-tree-level-order-traversal.py new file mode 100644 index 00000000..730fe9ac --- /dev/null +++ b/binary-tree-level-order-traversal.py @@ -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 \ No newline at end of file diff --git a/course-schedule.py b/course-schedule.py new file mode 100644 index 00000000..bd98b3d3 --- /dev/null +++ b/course-schedule.py @@ -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 \ No newline at end of file