diff --git a/BTLevelOrderTraversal.java b/BTLevelOrderTraversal.java new file mode 100644 index 00000000..4e140260 --- /dev/null +++ b/BTLevelOrderTraversal.java @@ -0,0 +1,48 @@ +import java.util.*; +/** + * 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 { + /* + TC - O(N) + SC - O(N) + This solution performs a level-order traversal of the binary tree using DFS (recursion). + Starting from the root at level 0, it recursively visits each node. If a level is being visited for the first time, + a new list is created for that level. The current node's value is added to the list corresponding to its level. + The recursion then continues with the left and right children, increasing the level by 1. + After all nodes are visited, result contains the values of the tree level by level. + */ + List> result = new ArrayList<>(); + + public List> levelOrder(TreeNode root) { + // TC - O(n) + // SC - O(n) (recursion stack uses O(h) space) + if (root == null) return result; + helper(root, 0); + return result; + } + + private void helper(TreeNode node, int level) { + if (node == null) return; + + if (level == result.size()) { + result.add(new ArrayList<>()); + } + + result.get(level).add(node.val); + helper(node.left, level + 1); + helper(node.right, level + 1); + } +} \ No newline at end of file diff --git a/CourseSchedule.java b/CourseSchedule.java new file mode 100644 index 00000000..ab8c52fd --- /dev/null +++ b/CourseSchedule.java @@ -0,0 +1,58 @@ +import java.util.*; + +class Solution { + /* + TC - O(V + E) + SC - O(V + E) + + A HashMap is used to build a graph where each course points to the courses that depend on it. + An indegree array stores the number of prerequisites for each course. + All courses with 0 prerequisites are added to a queue since they can be taken immediately. + The algorithm repeatedly removes a course from the queue, marks it as completed, + and reduces the indegree of its dependent courses. If any dependent course's indegree becomes 0, + it is added to the queue. If all numCourses are processed, there is no cycle, so all courses can + be finished. Otherwise, a cycle exists, making it impossible to complete all courses. + */ + public boolean canFinish(int numCourses, int[][] prerequisites) { + HashMap> map = new HashMap<>(); + int[] indegrees = new int[numCourses]; + for (int[] prerequisite : prerequisites) { + int de = prerequisite[0]; + int in = prerequisite[1]; + indegrees[de]++; + if (!map.containsKey(in)) { + map.put(in, new ArrayList<>()); + } + map.get(in).add(de); + } + + int count = 0; + + Queue queue = new LinkedList<>(); + for (int i = 0; i < numCourses; i++) { + if (indegrees[i] == 0) { + queue.add(i); + count++; + } + } + + if (count == numCourses) return true; + if (queue.isEmpty()) return false; + while (!queue.isEmpty()) { + int curr = queue.poll(); + List children = map.get(curr); + if (children != null) { + for (int child : children) { + indegrees[child]--; + if (indegrees[child] == 0) { + queue.add(child); + count++; + if (count == numCourses) return true; + } + } + } + } + + return false; + } +} \ No newline at end of file