Skip to content
Open

BFS 1 #1642

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
75 changes: 75 additions & 0 deletions BinaryTreeBFS.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Time Complexity : O(n) where n is the number of nodes in the tree
// Space Complexity : O(n) where n is the number of nodes in the tree
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No


// Your code here along with comments explaining your approach
// We will be using a queue to perform a level order traversal of the tree.
// We will keep track of the size of the queue at each level to know how many nodes are at that level.
// We will add the values of the nodes at each level to a list and add that list to the result list.
// We will continue this process until the queue is empty, which means we have traversed all the levels of the tree.
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 {

public List<List<Integer>> levelOrder(CourseScheduleBFS root) {

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

if (root == null) {
return result;
}

Queue<CourseScheduleBFS> que = new LinkedList<>();

que.offer(root);

while (!que.isEmpty()) {

int size = que.size();

List<Integer> li = new ArrayList<>();

for (int i = 0; i < size; i++) {

CourseScheduleBFS node = que.poll();

if (node.left != null) {
que.offer(node.left);
}

if (node.right != null) {
que.offer(node.right);
}

li.add(node.val);
}

result.add(li);
}

return result;
}
}
61 changes: 61 additions & 0 deletions BinaryTreeDFS.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Time Complexity : O(n) where n is the number of nodes in the tree
// Space Complexity : O(n) where n is the number of nodes in the tree STACK space is O(h) where h is the height of the tree
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No


// Your code here along with comments explaining your approach
// We will be using a helper function to perform a depth first traversal of the tree.
// We will keep track of the level of the tree we are currently on and add the values of the nodes at that level to a list.
// We will add that list to the result list at the end of the traversal.
// We will continue this process until we have traversed all the levels of the tree.
/**
* 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 {

List<List<Integer>> result;

public List<List<Integer>> levelOrder(TreeNode root) {

result = new ArrayList<>();

helper(root, 0);

return result;
}

public void helper(TreeNode root, int level) {

if (root == null) {
return;
}

if (result.size() == level) {
result.add(new ArrayList<>());
}

result.get(level).add(root.val);

helper(root.left, level + 1);
helper(root.right, level + 1);
}
}
68 changes: 68 additions & 0 deletions CourseScheduleBFS.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Time Complexity : O(E+V) where E is the number of edges and V is the number of vertices in the graph
// Space Complexity : o(E+V) where E is the number of edges and V is the number of vertices in the graph
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No


// Your code here along with comments explaining your approach
// Topological sort using BFS
// We will be using a queue to perform a breadth first traversal of the graph.
// We will keep track of the indegree of each vertex and add all vertices with indegree 0 to the queue.
// We will then process each vertex in the queue and reduce the indegree of its neighbors by 1.
// If any neighbor's indegree becomes 0, we will add it to the queue.
// We will keep track of the number of vertices processed and at the end, if the count is equal to the number of courses,
// it means we can finish all courses, otherwise we cannot.
import java.util.*;

class Solution {

public boolean canFinish(int numCourses, int[][] prerequisites) {

int[] indegrees = new int[numCourses];

Map<Integer, List<Integer>> map = new HashMap<>();

// Build graph and indegree array
for (int[] pr : prerequisites) {

indegrees[pr[0]]++;

map.putIfAbsent(pr[1], new ArrayList<>());

map.get(pr[1]).add(pr[0]);
}

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

// Add all courses with indegree 0
for (int i = 0; i < numCourses; i++) {

if (indegrees[i] == 0) {
que.offer(i);
}
}

int count = 0;

while (!que.isEmpty()) {

int curr = que.poll();

count++;

if (map.containsKey(curr)) {

for (int dependent : map.get(curr)) {

indegrees[dependent]--;

if (indegrees[dependent] == 0) {
que.offer(dependent);
}
}
}
}

return count == numCourses;
}
}