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
52 changes: 52 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* 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;
* }
* }
*/
/*
102. Binary Tree Level Order Traversal
Ran on leetcode: Yes
TC: O(n)
SC: O(n)
At each level process the node and add the node to the list and add all of its children in the queue, do the same till we have processed aall the nodes
*/
class Solution {

private List<List<Integer>> result = new ArrayList<>();

public List<List<Integer>> levelOrder(TreeNode root) {
bfs(root);
return result;
}

private void bfs(TreeNode root){
if(root == null) return; // if root node is null
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while(!queue.isEmpty()){ // check if queue is empty
int size = queue.size();
List<Integer> sameLevel = new ArrayList<>();
for(int i = 0; i < size; i++){
TreeNode node = queue.poll();
sameLevel.add(node.val); // add the node to the list
if(node.left != null){ // add left child if left child is not empty
queue.offer(node.left);
}
if(node.right != null){ // add right child if right child is not empty
queue.offer(node.right);
}
}
result.add(sameLevel);
}
}
}
62 changes: 62 additions & 0 deletions Problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
207. Course Schedule
Ran on leetcode: Yes
TC:O(V+E)
SC:O(V+E)
Create adj map of for faster search, create indegrees array indicating how many courses is it dependent on, add to the queue all the courses with indegrees 0 and then mark it as completed and update indegrees array if the value becomes 0 for the course enqueue it. If the number of courses that can be completed is num of courses return true
*/
class Solution {
public boolean canFinish(int numCourses, int[][] prerequisites) {
int[] indegress = new int[numCourses];
Map<Integer, List<Integer>> adjMap = new HashMap<>();
Queue<Integer> queue = new LinkedList<>();

for(int i = 0; i < prerequisites.length; i++){
int dependent = prerequisites[i][0];
int independent = prerequisites[i][1];
// number of dependent on each course
indegress[dependent]++;
// Create map key = independent, value = list of dependents
adjMap.putIfAbsent(independent, new ArrayList<>());
adjMap.get(independent).add(dependent);
}

int count = 0;
for(int i = 0; i < indegress.length; i++){ // enqueue all the elements which are fully independent
if(indegress[i] == 0){
count++;
queue.offer(i);
}
}

if(count == numCourses){ // All the elements are enqueued, then all the courses can be completed
return true;
}

if(count == 0){ // If no elements are enqueued, then there are no course from where we can start
return false;
}

while(!queue.isEmpty()){
int course = queue.poll();
List<Integer> dependents = adjMap.get(course);
if(dependents != null){
int size = dependents.size();
for(int i = 0; i < size; i++){
int temp = dependents.get(i);
indegress[temp]--; // Dependent course completed so reduce indegree to it
if(indegress[temp] == 0){ // Course can be completed
queue.offer(temp);
count++;
if(count == numCourses){ // All courses can be completed
return true;
}
}
}
}
}

return false;

}
}