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
101 changes: 101 additions & 0 deletions Leetcode_102.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
//way1
/**
* 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;
* }
* }
*/
//Place root node in queue
//Rootnode acts as parent
//Travel through queue based on it's size and add them to list
//Once travelled till size, add list to ans and continue with new size and so on
//ie., travelling parent - it's children - it's grandchildren so on
//TC: O(n); SC:O(n)
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> ans=new ArrayList<>();
if(root==null){
return ans;
}

Queue<TreeNode> queue=new LinkedList<>();
queue.add(root);
while(!(queue.isEmpty())){
int size=queue.size();
List<Integer> curr=new ArrayList<>();
for(int i=0;i<size;i++){
TreeNode currNode=queue.poll();
curr.add(currNode.val);

if(currNode.left!=null){
queue.add(currNode.left);
}

if(currNode.right!=null){
queue.add(currNode.right);
}

}

ans.add(curr);
}

return ans;


}
}

//way2
/**
* 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;
* }
* }
*/
//DFS
//Maintain ans as List of List --> reference
//Check level at every node
//If that level is already present in ans, add it to the subList of ans at level. Else, create new List, add element and add that list to ans
//TC: O(n); SC:O(h)
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> ans=new ArrayList<>();
helper(root,0,ans);
return ans;
}

private void helper(TreeNode root, int level, List<List<Integer>> ans){
if(root==null){
return;
}


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

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

helper(root.left,level+1,ans);
helper(root.right,level+1,ans);
}
}
63 changes: 63 additions & 0 deletions Leetcode_207.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//Topological sort
//Travel through prereq
//Maintain indegree array; Maintain map with key as indep node and value as list of it's dependents
//Once the indegree array and map is formed, check number of independent nodes from indegree array
//If all are independent - we can complete all courses
//Else, add independent nodes to queue
//Process one by one. Update indegree array every time.
//If all nodes of indegree array becomes 0, no cycle and courses can be completed
//TC: O(V+E)
//SC: O(V+E)

class Solution {
public boolean canFinish(int numCourses, int[][] prerequisites) {
int n=numCourses;
int[] indegree=new int[n];
Map<Integer,List<Integer>> map=new HashMap<>();

for(int[] req:prerequisites){
int dep=req[0];
int indep=req[1];

indegree[dep]+=1;

if(!(map.containsKey(indep))){
map.put(indep,new ArrayList<>());
}
map.get(indep).add(dep);
}

int count=0;
Queue<Integer> queue=new LinkedList<>();
for(int i=0;i<n;i++){
if(indegree[i]==0){
queue.add(i);
count+=1;
}
}

if(count==n) return true;

while(!(queue.isEmpty())){
int currVal=queue.poll();
List<Integer> curr=map.get(currVal);
if(curr!=null){
for(int c:curr){
indegree[c]-=1;
if(indegree[c]==0){
queue.add(c);
count+=1;

if(count==n) return true;
}
}
}
}

if(count==n) return true;
return false;



}
}