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
57 changes: 57 additions & 0 deletions BinaryTreeLevelOrderTraversal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Time Complexity : O(n)
// Space Complexity : O(W) - width 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
/*
Have a queue where we initially add root and poll the elements level by level by taking snapshot of the
size of the queue. We also make sure to add current node's left and right nodes if available. This way of
using queue's FIFO principle to process the nodes and also taking snapshot approach helps us traverse
elements in a level order way.
*/
/**
* 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) {
this.result = new ArrayList<>();
traverseListHelper(root);
return result;
}

private void traverseListHelper(TreeNode root) {
if(root == null)
return;
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);

while(!queue.isEmpty()) {
int size = queue.size();
List<Integer> nodeList = new ArrayList<>();
for(int i = 0 ; i < size ; i++) {
TreeNode temp = queue.poll();
nodeList.add(temp.val);

if(temp.left != null)
queue.add(temp.left);
if(temp.right != null)
queue.add(temp.right);
}
result.add(nodeList);
}
}
}
61 changes: 61 additions & 0 deletions CourseSchedule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Time Complexity : O(V + E) / O(N + P)
// Space Complexity : O(V + E) / O(N + P)
// 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
/*
when we try to draw the dependencies of this problem, we can see its a graph and idea is to identify if
there is a cycle present.If cycle is present, we cant finish all courses.So, firstly, we construct an adjacency
map and have an indegree array to optimize the traversal.Then, we have a queue and add the independent courses
first and then traverse through that course's dependent courses using adjacency map and add those courses to
the queue if they become independent.At last, we check if our count has reached to the required number of courses or not.
*/
class Solution {
public boolean canFinish(int numCourses, int[][] prerequisites) {
int[] indegree = new int[numCourses];
Map<Integer, List<Integer>> map = new HashMap<>();

//arr[0] - dependent course, arr[1] - independent course
for(int[] arr : prerequisites) {
//increment dependent course values
indegree[arr[0]]++;
//map of independent to dependent courses
map.putIfAbsent(arr[1], new ArrayList<>());
map.get(arr[1]).add(arr[0]);
}

Queue<Integer> queue = new LinkedList<>();
int count = 0;

//add independent courses first to the queue (behaviour like root)
for(int i = 0 ; i < indegree.length ; i++) {
if(indegree[i] == 0) {
queue.add(i);
count++;
}
}

//no independent courses at all -> cycle
if(queue.isEmpty())
return false;
//all are independent courses
if(count == numCourses)
return true;

while(!queue.isEmpty()) {
int course = queue.poll();
List<Integer> dependentList = map.get(course);
if(dependentList != null) {
for(int dependent : dependentList) {
indegree[dependent]--;
if(indegree[dependent] == 0) {
queue.add(dependent);
count++;
}
}
}
}
return count == numCourses ? true : false;
}
}