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
41 changes: 41 additions & 0 deletions Binary-tree-level-order-traversal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* 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 {
//BFS
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> result = new ArrayList<>();
//Q is an interface. So it is not new Q<>()
Queue<TreeNode> q = new LinkedList<>();

if(root == null) return result;
//Q push is offer
q.offer(root);
while(!q.isEmpty()){
int size = q.size();
List<Integer> list = new ArrayList<>();
for(int i=0;i<size;i++){
TreeNode node = q.poll();
list.add(node.val);
if(node.left!=null) q.offer(node.left);
if(node.right!=null) q.offer(node.right);
}

result.add(list);

}
return result;
}
}
50 changes: 50 additions & 0 deletions Course-schedule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
class Solution {
public boolean canFinish(int numCourses, int[][] prerequisites) {
HashMap<Integer, List<Integer>> map = new HashMap<>();
int[] indegarr = new int[numCourses];
for(int[] prerequisite: prerequisites){
int indep = prerequisite[1];
int dep = prerequisite[0];
indegarr[dep]++; //in degree array will keep all inwards arrow count
//creating the hashmap
if(!map.containsKey(indep)){
//create new list
map.put(indep, new ArrayList<>());

}
map.get(indep).add(dep);
}

Queue<Integer> q = new LinkedList<>();

int count = 0;
for(int i = 0 ;i<numCourses; i++){
//if no dep add it to q
if(indegarr[i]== 0){
q.add(i);
count++;
}
}

if(count==numCourses) return true;
while(!q.isEmpty()){
int curr = q.poll();
//look for dependents in map
List<Integer> children = map.get(curr);
//reduce their indegrees by 1
if(children!=null){
for( int child: children){
indegarr[child]--;
if(indegarr[child]==0){
q.add(child);
count++;
}
if(count==numCourses) return true;
}
}


}
return false;
}
}
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# BFS-1
# Problem 1
Binary Tree Level Order Traversal (https://leetcode.com/problems/binary-tree-level-order-traversal/)
* BFS solution - add the root element in the queue first. Assign the size of queue & list for every level.
* pop the element, add to the list & add children to the queue.
* T- O(n) all nodes to be iterated, S - O(n) width of the tree.

# Problem 2
Course Schedule (https://leetcode.com/problems/course-schedule/)

* DS1 - in degree array will keep all inwards arrow count, DS2 - HashMap(indep, list of dep), DS3 - Queue that will store V with indegrees[]==0
* In the Q, look for dependents in map, reduce indegree by 1, Queue that will store V with indegrees[]==0
* if count=numCourses means we can finish all courses.