From 605723e1e2e7bc0f679c794fc4ad921c70507908 Mon Sep 17 00:00:00 2001 From: Sarvani Baru Date: Tue, 3 Mar 2026 13:22:16 -0800 Subject: [PATCH] Done BFS-1 --- BinaryTreeLevelOrderTraversal.java | 57 ++++++++++++++++++++++++++++ CourseSchedule.java | 61 ++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 BinaryTreeLevelOrderTraversal.java create mode 100644 CourseSchedule.java diff --git a/BinaryTreeLevelOrderTraversal.java b/BinaryTreeLevelOrderTraversal.java new file mode 100644 index 00000000..7ef9b575 --- /dev/null +++ b/BinaryTreeLevelOrderTraversal.java @@ -0,0 +1,57 @@ +// Time Complexity : O(n) +// Space Complexity : O(W) - width of the tree +// Did this code successfully run on Leetcode : yes +// Any problem you faced while coding this : no + +// Your code here along with comments explaining your approach +/* +Have a queue where we initially add root and poll the elements level by level by taking snapshot of the +size of the queue. We also make sure to add current node's left and right nodes if available. This way of +using queue's FIFO principle to process the nodes and also taking snapshot approach helps us traverse +elements in a level order way. + */ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode() {} + * TreeNode(int val) { this.val = val; } + * TreeNode(int val, TreeNode left, TreeNode right) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ +class Solution { + List> result; + public List> levelOrder(TreeNode root) { + this.result = new ArrayList<>(); + traverseListHelper(root); + return result; + } + + private void traverseListHelper(TreeNode root) { + if(root == null) + return; + Queue queue = new LinkedList<>(); + queue.add(root); + + while(!queue.isEmpty()) { + int size = queue.size(); + List nodeList = new ArrayList<>(); + for(int i = 0 ; i < size ; i++) { + TreeNode temp = queue.poll(); + nodeList.add(temp.val); + + if(temp.left != null) + queue.add(temp.left); + if(temp.right != null) + queue.add(temp.right); + } + result.add(nodeList); + } + } +} \ No newline at end of file diff --git a/CourseSchedule.java b/CourseSchedule.java new file mode 100644 index 00000000..d5a32588 --- /dev/null +++ b/CourseSchedule.java @@ -0,0 +1,61 @@ +// Time Complexity : O(V + E) / O(N + P) +// Space Complexity : O(V + E) / O(N + P) +// Did this code successfully run on Leetcode : yes +// Any problem you faced while coding this : no + +// Your code here along with comments explaining your approach +/* +when we try to draw the dependencies of this problem, we can see its a graph and idea is to identify if +there is a cycle present.If cycle is present, we cant finish all courses.So, firstly, we construct an adjacency +map and have an indegree array to optimize the traversal.Then, we have a queue and add the independent courses +first and then traverse through that course's dependent courses using adjacency map and add those courses to +the queue if they become independent.At last, we check if our count has reached to the required number of courses or not. + */ +class Solution { + public boolean canFinish(int numCourses, int[][] prerequisites) { + int[] indegree = new int[numCourses]; + Map> map = new HashMap<>(); + + //arr[0] - dependent course, arr[1] - independent course + for(int[] arr : prerequisites) { + //increment dependent course values + indegree[arr[0]]++; + //map of independent to dependent courses + map.putIfAbsent(arr[1], new ArrayList<>()); + map.get(arr[1]).add(arr[0]); + } + + Queue queue = new LinkedList<>(); + int count = 0; + + //add independent courses first to the queue (behaviour like root) + for(int i = 0 ; i < indegree.length ; i++) { + if(indegree[i] == 0) { + queue.add(i); + count++; + } + } + + //no independent courses at all -> cycle + if(queue.isEmpty()) + return false; + //all are independent courses + if(count == numCourses) + return true; + + while(!queue.isEmpty()) { + int course = queue.poll(); + List dependentList = map.get(course); + if(dependentList != null) { + for(int dependent : dependentList) { + indegree[dependent]--; + if(indegree[dependent] == 0) { + queue.add(dependent); + count++; + } + } + } + } + return count == numCourses ? true : false; + } +} \ No newline at end of file