From c3671cbfa1173d2beabc3d5da6ee0c36a4bc4537 Mon Sep 17 00:00:00 2001 From: Sreeja-99 <75175169+Sreeja-99@users.noreply.github.com> Date: Tue, 20 Jan 2026 23:08:57 -0600 Subject: [PATCH 1/2] Add course completion check using topological sort Implement topological sort to determine if all courses can be completed based on prerequisites. --- Leetcode_207.java | 63 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 Leetcode_207.java diff --git a/Leetcode_207.java b/Leetcode_207.java new file mode 100644 index 00000000..31c6002a --- /dev/null +++ b/Leetcode_207.java @@ -0,0 +1,63 @@ +//Topological sort +//Travel through prereq +//Maintain indegree array; Maintain map with key as indep node and value as list of it's dependents +//Once the indegree array and map is formed, check number of independent nodes from indegree array +//If all are independent - we can complete all courses +//Else, add independent nodes to queue +//Process one by one. Update indegree array every time. +//If all nodes of indegree array becomes 0, no cycle and courses can be completed +//TC: O(V+E) +//SC: O(V+E) + +class Solution { + public boolean canFinish(int numCourses, int[][] prerequisites) { + int n=numCourses; + int[] indegree=new int[n]; + Map> map=new HashMap<>(); + + for(int[] req:prerequisites){ + int dep=req[0]; + int indep=req[1]; + + indegree[dep]+=1; + + if(!(map.containsKey(indep))){ + map.put(indep,new ArrayList<>()); + } + map.get(indep).add(dep); + } + + int count=0; + Queue queue=new LinkedList<>(); + for(int i=0;i curr=map.get(currVal); + if(curr!=null){ + for(int c:curr){ + indegree[c]-=1; + if(indegree[c]==0){ + queue.add(c); + count+=1; + + if(count==n) return true; + } + } + } + } + + if(count==n) return true; + return false; + + + + } +} From 44c4b5b130673a1e231ba194dc45d0f23796b67d Mon Sep 17 00:00:00 2001 From: Sreeja-99 <75175169+Sreeja-99@users.noreply.github.com> Date: Tue, 20 Jan 2026 23:10:03 -0600 Subject: [PATCH 2/2] Implement level order traversal for binary tree --- Leetcode_102.java | 101 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 Leetcode_102.java diff --git a/Leetcode_102.java b/Leetcode_102.java new file mode 100644 index 00000000..8c5316e4 --- /dev/null +++ b/Leetcode_102.java @@ -0,0 +1,101 @@ +//way1 +/** + * 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; + * } + * } + */ + //Place root node in queue + //Rootnode acts as parent + //Travel through queue based on it's size and add them to list + //Once travelled till size, add list to ans and continue with new size and so on + //ie., travelling parent - it's children - it's grandchildren so on + //TC: O(n); SC:O(n) +class Solution { + public List> levelOrder(TreeNode root) { + List> ans=new ArrayList<>(); + if(root==null){ + return ans; + } + + Queue queue=new LinkedList<>(); + queue.add(root); + while(!(queue.isEmpty())){ + int size=queue.size(); + List curr=new ArrayList<>(); + for(int i=0;i reference + //Check level at every node + //If that level is already present in ans, add it to the subList of ans at level. Else, create new List, add element and add that list to ans + //TC: O(n); SC:O(h) +class Solution { + public List> levelOrder(TreeNode root) { + List> ans=new ArrayList<>(); + helper(root,0,ans); + return ans; + } + + private void helper(TreeNode root, int level, List> ans){ + if(root==null){ + return; + } + + + if(level==ans.size()){ + ans.add(new ArrayList<>()); + } + + ans.get(level).add(root.val); + + helper(root.left,level+1,ans); + helper(root.right,level+1,ans); + } +}