diff --git a/Problem1.cs b/Problem1.cs new file mode 100644 index 00000000..7cb4e20e --- /dev/null +++ b/Problem1.cs @@ -0,0 +1,70 @@ +// Time Complexity : O(n) +// Space Complexity : O(Width of tree) = Maximum size of elements that can be present in the queue +// 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 + +/* + If root is null I return the resultant list of integer lists as it is. If not, I enqueue the root node to a queue. I iterate through the tree nodes while the queue is not empty for i = size of + queue times. This allows me to distinguish between levels of the tree. For each node in the queue, I dequeue it and add it's value to the temp list. If it has any left or right child, I add them + to the queue. At the end of the inner for loop, I add the temporary list to the final resultant list. +*/ + +/** + * Definition for a binary tree node. + * public class TreeNode { + * public int val; + * public TreeNode left; + * public TreeNode right; + * public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ +public class Solution +{ + public IList> LevelOrder(TreeNode root) + { + List> result = new(); + + if (root == null) + { + return result; + } + + Queue q = new(); + + q.Enqueue(root); + + while (q.Count > 0) + { + int size = q.Count; + + List tempList = new(); + + for (int i = 0; i < size; i++) + { + TreeNode temp = q.Dequeue(); + tempList.Add(temp.val); + + if (temp.left != null) + { + q.Enqueue(temp.left); + } + + if (temp.right != null) + { + q.Enqueue(temp.right); + } + } + + result.Add(tempList); + } + + return result; + } +} \ No newline at end of file diff --git a/Problem2.cs b/Problem2.cs new file mode 100644 index 00000000..7ad3f865 --- /dev/null +++ b/Problem2.cs @@ -0,0 +1,70 @@ +// Time Complexity : O(m+n) +// Space Complexity : O(m+n) +// 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 + +/* + +*/ + +public class Solution { + public bool CanFinish(int numCourses, int[][] prerequisites) { + int count = 0; + Dictionary> adjMap = new(); + int[] inwardCount = new int[numCourses]; + + foreach(int[] prereq in prerequisites) + { + inwardCount[prereq[1]] += 1; + + if(!adjMap.ContainsKey(prereq[0])) + { + adjMap[prereq[0]] = new List(); + } + + adjMap[prereq[0]].Add(prereq[1]); + } + + Queue q = new(); + + for(int i = 0; i < inwardCount.Length; i++) + { + if(inwardCount[i] == 0) + { + q.Enqueue(i); + count += 1; + } + } + + if(q.Count == 0) + { + return false; + } + + while(q.Count != 0) + { + int currentCourse = q.Dequeue(); + + if(adjMap.ContainsKey(currentCourse)) + { + List nextCourses = adjMap[currentCourse]; + + foreach(int course in nextCourses) + { + inwardCount[course] -= 1; + if(inwardCount[course] == 0) + { + q.Enqueue(course); + count += 1; + } + } + } + + } + + return count == numCourses; + } +} \ No newline at end of file