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
39 changes: 39 additions & 0 deletions Problem-1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Time Complexity :O(N)
// Space Complexity :O(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
// We use a queue (deque) to perform BFS, traversing the tree level by level.
// For each level, we iterate over all nodes currently in the queue, appending their values to a list.
// Children of nodes are added to the queue, and each level’s list of values is added to the result.

class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> result = new ArrayList<>();
if (root == null) return result;

Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);

while (!queue.isEmpty()) {
int n = queue.size();
List<Integer> level = new ArrayList<>();

for (int i = 0; i < n; i++) {
TreeNode node = queue.poll();
level.add(node.val);

if (node.left != null) {
queue.add(node.left);
}
if (node.right != null) {
queue.add(node.right);
}
}
result.add(level);
}

return result;
}
}
38 changes: 38 additions & 0 deletions Problem-1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Time Complexity :O(N)
# Space Complexity :O(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
# We use a queue (deque) to perform BFS, traversing the tree level by level.
# For each level, we iterate over all nodes currently in the queue, appending their values to a list.
# Children of nodes are added to the queue, and each level’s list of values is added to the result.

# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
stack = deque()
res = []
if root != None:
stack.append(root)
while stack:
n = len(stack)
out = []
for i in range(n):
node = stack.popleft()
out.append(node.val)
if node.left:
stack.append(node.left)
if node.right:
stack.append(node.right)
res.append(out)
return res




58 changes: 58 additions & 0 deletions Problem-2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Time Complexity :O(N+P) N is the length of prere array
// Space Complexity :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

// We use a Queue to perform BFS, traversing the tree level by level.
// For each level, we iterate over the current size of the queue, processing all nodes at that level.
// Children of nodes are added to the queue, and the values of each level are stored in a list.

import java.util.*;

class Solution {
public boolean canFinish(int numCourses, int[][] prerequisites) {
// adjacency list: prereq -> list of courses depending on it
List<List<Integer>> adj = new ArrayList<>();
for (int i = 0; i < numCourses; i++) {
adj.add(new ArrayList<>());
}

// indegree array: number of prerequisites for each course
int[] indegree = new int[numCourses];

// build graph
for (int[] pre : prerequisites) {
int course = pre[0];
int prereq = pre[1];
adj.get(prereq).add(course);
indegree[course]++;
}

// queue of courses with no prerequisites
Queue<Integer> queue = new LinkedList<>();
for (int i = 0; i < numCourses; i++) {
if (indegree[i] == 0) {
queue.add(i);
}
}

// BFS topological sort
int visited = 0;
while (!queue.isEmpty()) {
int curr = queue.poll();
visited++;

for (int next : adj.get(curr)) {
indegree[next]--;
if (indegree[next] == 0) {
queue.add(next);
}
}
}

// if we visited all courses, no cycle exists
return visited == numCourses;
}
}
38 changes: 38 additions & 0 deletions Problem-2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Time Complexity :O(N+P) N is the length of prere array
# Space Complexity :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

# We use a Queue to perform BFS, traversing the tree level by level.
# For each level, we iterate over the current size of the queue, processing all nodes at that level.
# Children of nodes are added to the queue, and the values of each level are stored in a list.

from collections import deque

class Solution:
def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
pre_map = {}
indegree = [0] * numCourses
queue = deque()

for course, prereq in prerequisites:
if prereq not in pre_map:
pre_map[prereq] = []
pre_map[prereq].append(course)
indegree[course] += 1

for i in range(numCourses):
if indegree[i] == 0:
queue.append(i)

while queue:
curr = queue.popleft()
if curr in pre_map:
for next_course in pre_map[curr]:
indegree[next_course] -= 1
if indegree[next_course] == 0:
queue.append(next_course)

return all(x == 0 for x in indegree)