Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions BTLevelOrderTraversal.java
Original file line number Diff line number Diff line change
@@ -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<List<Integer>> result = new ArrayList<>();

public List<List<Integer>> 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);
}
}
58 changes: 58 additions & 0 deletions CourseSchedule.java
Original file line number Diff line number Diff line change
@@ -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<Integer, List<Integer>> 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<Integer> 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<Integer> 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;
}
}