From d139d991e1cf54fe2c7beee97c131d02c21b6c33 Mon Sep 17 00:00:00 2001 From: Dheepthi-Reddy Date: Fri, 19 Dec 2025 14:17:54 -0600 Subject: [PATCH] Solved BFS-1 --- BinaryTreeLevelOrderTraversal.py | 37 ++++++++++++++++++++ CourseSchedule.py | 59 ++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 BinaryTreeLevelOrderTraversal.py create mode 100644 CourseSchedule.py diff --git a/BinaryTreeLevelOrderTraversal.py b/BinaryTreeLevelOrderTraversal.py new file mode 100644 index 00000000..97b59787 --- /dev/null +++ b/BinaryTreeLevelOrderTraversal.py @@ -0,0 +1,37 @@ +''' +In this problem, given a binary tree, we need to find the level order traversal nodes. +To solve this, I took a queue, since queue works as FIFO, as I popped the first element and stored it in a temporary array, and its child nodes to the end of queue. +As we pop the elements, I took a size variable on queue, and maintained the list of lists. +The no.of elements of queue at the that time equal to no.of elements on the list/level of the tree. +So, every time we iterate on the tree as many times as the size, and create lists. +''' +class Solution: + def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: + if root is None: return [] + + result = [] + queue = [root] + # as long as we have elements in the queue + while queue: + size = len(queue) + temp = [] + for i in range(size): + # popping the first element and adding to a temporary list + curr = queue.pop(0) + temp.append(curr.val) + # adding children of each node to the queue as long as they exist + if curr.left: + queue.append(curr.left) + if curr.right: + queue.append(curr.right) + # adding the list of nodes to the result list + result.append(temp) + + return result + +''' +Time Complexity: O(n) +Because we are traversing through all the nodes once. +Time Complexity: O(n) +Maximum no of nodes we can have in the queue at a time is equal to the width of the tree, which is n/2, since we can ignore constants O(n) +''' \ No newline at end of file diff --git a/CourseSchedule.py b/CourseSchedule.py new file mode 100644 index 00000000..d41a5c2f --- /dev/null +++ b/CourseSchedule.py @@ -0,0 +1,59 @@ +''' +Given an array of courses and its prerequisites, we have to return if we can finish all the courses or not. +To finish all the courses, we first have to start with a course that has no prerequisites, store that in a queue. +We can store the count of each course dependencies in a indegree array, to get the order of the prerequisites. +As soon as we are done with the independent course we can pop the course and add the courses which are dependent on this, parallaly we update the counts in indegree array. +inorder not to iterate on the tree everytime for going through the courses, we can maintain the adjecency map to store the independent courses as keys and dependent course arrays as values +we update the queue and indegree for every iteration, if length of queue becomes equal to the no of courses, we are finished with all the courses. +''' +class Solution: + def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool: + indegrees = [0] * numCourses + graph = {} + + for p in prerequisites: + # incrasing indegree of dependent course + indegrees[p[0]] += 1 + # mapping courses to its prerequisites + if p[1] not in graph: + graph[p[1]] = [] + graph[p[1]].append(p[0]) + + count = 0 + q = deque() + + # iterating on courses + for i in range(numCourses): + # whichever course indegree is 0 is added to queue + if indegrees[i] == 0: + q.append(i) + count += 1 + + if not q: + return False + if count == numCourses: + return True + + while q: + # popping the first course + curr = q.popleft() + # getting dependent courses existing on the popped course + children = graph.get(curr) + # checking if it has any children to iterate + if children: + for child in children: + # reducing indegrees count + indegrees[child] -= 1 + # if its zero add to the queue + if indegrees[child] == 0: + q.append(child) + count += 1 + if count == numCourses: + return True + return False +''' +Time Complexity: O(V+E) +maximum time taken is the no of courses and number of edges +Space Complexity: O(V+E) +space consumed by the indegrees array is V and by the map is E +''' \ No newline at end of file