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
74 changes: 74 additions & 0 deletions binary-tree-level-order-traversal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Solution -1

// TimeComplexity: O(n) where n is number of nodes
// SpaceComplexity: O(h) where h is height of the tree.
// Explantion: I perform a DFS traversal while keeping track of the current depth (level). When visiting a node, if this is the first time reaching that level,
// I create a new list and add it to the result; otherwise, I append the value to the existing list for that level.
// I then recursively process the left and right children with level + 1, which ensures all nodes are grouped by depth in level-order form.



class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
if(root == null) return new ArrayList<>();
List<List<Integer>> result = new ArrayList<>();
helper(root, 0, result);
return result;
}

private void helper(TreeNode root, int level, List<List<Integer>> result){
// base
if(root == null) return;

// logic
List<Integer> inter;
if(level == result.size()) {
inter = new ArrayList<>();
result.add(inter);
} else {
inter = result.get(level);
}

inter.add(root.val);

// children

helper(root.left, level+1, result);
helper(root.right, level+1, result);

return;

}
}


//Solution-2

// TimeComplexity: O(n) where n is number of nodes
// SpaceComplexity: O(w) where w is width of the tree.
// Explanation: I use a breadth-first search with a queue to traverse the tree level by level. For each iteration, I record the current queue size, which represents the
// number of nodes at that level, process exactly those nodes, store their values in a list, and enqueue their left and right children.
// This groups nodes naturally by depth and produces the level-order result.


class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
if(root == null) return new ArrayList<>();
List<List<Integer>> result = new ArrayList<>();
Queue<TreeNode> q = new LinkedList<>();
q.add(root);
while(!q.isEmpty()) {
int size = q.size();
List<Integer> inter = new ArrayList<>();
for(int i =0; i<size; i++) {
TreeNode curr = q.poll();
inter.add(curr.val);
if(curr.left != null) q.add(curr.left);
if(curr.right != null) q.add(curr.right);
}
result.add(inter);
}
return result;

}
}
53 changes: 53 additions & 0 deletions course-schedule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// TimeComplexity: O(V+E) where V is numCourses and E is prerequisites
// SpaceComplexity: O(V+E)
// Explanation: I model the courses as a directed graph where an edge from B → A means course B must be taken before A. I build an adjacency list and an indegree
// array for each course. Then I push all courses with indegree 0 into a queue. Using BFS (Kahn’s algorithm), I repeatedly remove a course from the queue,
// reduce the indegree of its neighbors, and enqueue any neighbor whose indegree becomes 0. If I am able to process all courses, there is no cycle
// and it is possible to finish them; otherwise, a cycle exists.


class Solution {
public boolean canFinish(int numCourses, int[][] prerequisites) {
Map<Integer, List<Integer>> map = new HashMap<>();
int[] arr = new int[numCourses];
int count = 0;
for(int i=0; i<prerequisites.length; i++){
if(map.get(prerequisites[i][1])==null) {
List<Integer> list = new ArrayList<>();
list.add(prerequisites[i][0]);
map.put(prerequisites[i][1], list);
} else{
List<Integer> exist = map.get(prerequisites[i][1]);
exist.add(prerequisites[i][0]);
}
arr[prerequisites[i][0]]+=1;
}
Queue<Integer> q = new LinkedList<>();
for(int i=0; i<numCourses; i++){
if(arr[i] == 0) {
q.add(i);
}
}
while(!q.isEmpty()) {
int curr = q.poll();
count++;
List<Integer> currList = map.get(curr);
if(currList!=null){
for(int i =0; i<currList.size(); i++) {
int remove = currList.get(i);
arr[remove]-=1;
if(arr[remove] ==0) {
q.add(remove);
}
}
map.remove(curr);
}

}
if(count == numCourses) {
return true;
} else{
return false;
}
}
}