diff --git a/BinaryTreeBFS.java b/BinaryTreeBFS.java new file mode 100644 index 00000000..51fd335d --- /dev/null +++ b/BinaryTreeBFS.java @@ -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> levelOrder(CourseScheduleBFS root) { + + List> result = new ArrayList<>(); + + if (root == null) { + return result; + } + + Queue que = new LinkedList<>(); + + que.offer(root); + + while (!que.isEmpty()) { + + int size = que.size(); + + List 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; + } +} \ No newline at end of file diff --git a/BinaryTreeDFS.java b/BinaryTreeDFS.java new file mode 100644 index 00000000..dada4db0 --- /dev/null +++ b/BinaryTreeDFS.java @@ -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> result; + + public List> 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); + } +} \ No newline at end of file diff --git a/CourseScheduleBFS.java b/CourseScheduleBFS.java new file mode 100644 index 00000000..259f92a7 --- /dev/null +++ b/CourseScheduleBFS.java @@ -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> 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 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; + } +} \ No newline at end of file