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
70 changes: 70 additions & 0 deletions Problem1.cs
Original file line number Diff line number Diff line change
@@ -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<IList<int>> LevelOrder(TreeNode root)
{
List<IList<int>> result = new();

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

Queue<TreeNode> q = new();

q.Enqueue(root);

while (q.Count > 0)
{
int size = q.Count;

List<int> 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;
}
}
70 changes: 70 additions & 0 deletions Problem2.cs
Original file line number Diff line number Diff line change
@@ -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<int, List<int>> 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<int>();
}

adjMap[prereq[0]].Add(prereq[1]);
}

Queue<int> 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<int> nextCourses = adjMap[currentCourse];

foreach(int course in nextCourses)
{
inwardCount[course] -= 1;
if(inwardCount[course] == 0)
{
q.Enqueue(course);
count += 1;
}
}
}

}

return count == numCourses;
}
}