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
93 changes: 93 additions & 0 deletions problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@

/**
Time Complexity : O(N)
Explanation:
Each node in the tree is visited exactly once.

Space Complexity : O(N)
Explanation:
In the worst case, the recursion stack or queue can hold up to N nodes
(for skewed trees or wide levels).

Did this code successfully run on LeetCode : Yes

Any problem you faced while coding this :
Initially implemented the BFS approach using a queue.
Later implemented a DFS solution by tracking the level of each node.
Needed to check if the current level already exists in the result list;
if not, create a new list for that level before adding the node value.
*/

/**
* 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 {



public List<List<Integer>> levelOrder(TreeNode root) {

List<List<Integer>> result = new ArrayList<>();
dfs(root, 0, result);
return result;
}

// BFS approach (optional alternative)
private List<List<Integer>> bfs(TreeNode root) {

Queue<TreeNode> q = new LinkedList<>();
List<List<Integer>> li = new ArrayList<>();

if (root == null) return li;

q.add(root);

while (!q.isEmpty()) {

int size = q.size();
List<Integer> temp = new ArrayList<>();

for (int i = 0; i < size; i++) {

TreeNode t = q.poll();
temp.add(t.val);

if (t.left != null) q.add(t.left);
if (t.right != null) q.add(t.right);
}

li.add(temp);
}

return li;
}

// DFS approach used in solution
private void dfs(TreeNode root, int level, List<List<Integer>> result) {

if (root == null) return;

// Create new level list if needed
if (level == result.size()) {
result.add(new ArrayList<>());
}

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

dfs(root.left, level + 1, result);
dfs(root.right, level + 1, result);
}
}
94 changes: 94 additions & 0 deletions problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
Time Complexity : O(V + E)
Explanation:
V = number of courses
E = number of prerequisite relations
We build the graph once and process each node and edge once
using Kahn’s Algorithm (Topological Sort with BFS).

Space Complexity : O(V + E)
Explanation:
- HashMap stores adjacency list of the graph
- Queue stores nodes with 0 indegree
- indegree array stores dependency count

Did this code successfully run on LeetCode : Yes

Any problem you faced while coding this :
Initially confused about how to represent course dependencies.
Later implemented it as a directed graph where:
prerequisite → dependent course
Then applied Kahn’s Algorithm using indegree tracking.
Also needed a counter to check if all courses are processed,
which confirms there is no cycle.
*/

class Solution {



public boolean canFinish(int numCourses, int[][] prerequisites) {

Queue<Integer> q = new LinkedList<>();
int n = numCourses;

int[] inDeg = new int[n];
HashMap<Integer, List<Integer>> map = new HashMap<>();

int cnt = 0;

// Build graph and indegree array
for (int[] edge : prerequisites) {

int dep = edge[0];
int ind = edge[1];

if (!map.containsKey(ind)) {
map.put(ind, new ArrayList<>());
}

map.get(ind).add(dep);
inDeg[dep]++;
}

// Add nodes with indegree 0 to queue
for (int i = 0; i < n; i++) {
if (inDeg[i] == 0) {
q.add(i);
cnt++;
}
}

if (cnt == n) return true;
if (map.isEmpty()) return true;

// BFS for topological sort
while (!q.isEmpty()) {

int size = q.size();

for (int i = 0; i < size; i++) {

int curr = q.poll();
List<Integer> depli = map.get(curr);

if (depli != null) {

for (int k : depli) {

inDeg[k]--;

if (inDeg[k] == 0) {
q.add(k);
cnt++;

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

return false;
}
}