diff --git a/problem1.java b/problem1.java new file mode 100644 index 00000000..dd10cf80 --- /dev/null +++ b/problem1.java @@ -0,0 +1,93 @@ + + /** + Time Complexity : O(N) + Explanation: + Each node in the tree is visited exactly once. + + Space Complexity : O(N) + Explanation: + In the worst case, the recursion stack or queue can hold up to N nodes + (for skewed trees or wide levels). + + Did this code successfully run on LeetCode : Yes + + Any problem you faced while coding this : + Initially implemented the BFS approach using a queue. + Later implemented a DFS solution by tracking the level of each node. + Needed to check if the current level already exists in the result list; + if not, create a new list for that level before adding the node value. + */ + +/** + * 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 { + + + + public List> levelOrder(TreeNode root) { + + List> result = new ArrayList<>(); + dfs(root, 0, result); + return result; + } + + // BFS approach (optional alternative) + private List> bfs(TreeNode root) { + + Queue q = new LinkedList<>(); + List> li = new ArrayList<>(); + + if (root == null) return li; + + q.add(root); + + while (!q.isEmpty()) { + + int size = q.size(); + List temp = new ArrayList<>(); + + for (int i = 0; i < size; i++) { + + TreeNode t = q.poll(); + temp.add(t.val); + + if (t.left != null) q.add(t.left); + if (t.right != null) q.add(t.right); + } + + li.add(temp); + } + + return li; + } + + // DFS approach used in solution + private void dfs(TreeNode root, int level, List> result) { + + if (root == null) return; + + // Create new level list if needed + if (level == result.size()) { + result.add(new ArrayList<>()); + } + + result.get(level).add(root.val); + + dfs(root.left, level + 1, result); + dfs(root.right, level + 1, result); + } +} \ No newline at end of file diff --git a/problem2.java b/problem2.java new file mode 100644 index 00000000..6f29902d --- /dev/null +++ b/problem2.java @@ -0,0 +1,94 @@ + /** + Time Complexity : O(V + E) + Explanation: + V = number of courses + E = number of prerequisite relations + We build the graph once and process each node and edge once + using Kahn’s Algorithm (Topological Sort with BFS). + + Space Complexity : O(V + E) + Explanation: + - HashMap stores adjacency list of the graph + - Queue stores nodes with 0 indegree + - indegree array stores dependency count + + Did this code successfully run on LeetCode : Yes + + Any problem you faced while coding this : + Initially confused about how to represent course dependencies. + Later implemented it as a directed graph where: + prerequisite → dependent course + Then applied Kahn’s Algorithm using indegree tracking. + Also needed a counter to check if all courses are processed, + which confirms there is no cycle. + */ + + class Solution { + + + + public boolean canFinish(int numCourses, int[][] prerequisites) { + + Queue q = new LinkedList<>(); + int n = numCourses; + + int[] inDeg = new int[n]; + HashMap> map = new HashMap<>(); + + int cnt = 0; + + // Build graph and indegree array + for (int[] edge : prerequisites) { + + int dep = edge[0]; + int ind = edge[1]; + + if (!map.containsKey(ind)) { + map.put(ind, new ArrayList<>()); + } + + map.get(ind).add(dep); + inDeg[dep]++; + } + + // Add nodes with indegree 0 to queue + for (int i = 0; i < n; i++) { + if (inDeg[i] == 0) { + q.add(i); + cnt++; + } + } + + if (cnt == n) return true; + if (map.isEmpty()) return true; + + // BFS for topological sort + while (!q.isEmpty()) { + + int size = q.size(); + + for (int i = 0; i < size; i++) { + + int curr = q.poll(); + List depli = map.get(curr); + + if (depli != null) { + + for (int k : depli) { + + inDeg[k]--; + + if (inDeg[k] == 0) { + q.add(k); + cnt++; + + if (cnt == n) return true; + } + } + } + } + } + + return false; + } + } \ No newline at end of file