From feeed61dae077f082e3a37de90d7c102ea923b1c Mon Sep 17 00:00:00 2001 From: Sahithipsl470 Date: Wed, 28 Jan 2026 20:27:02 -0500 Subject: [PATCH] "BST-1 done" --- Problem-1.java | 39 +++++++++++++++++++++++++++++++++ Problem-1.py | 38 +++++++++++++++++++++++++++++++++ Problem-2.java | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ Problem-2.py | 38 +++++++++++++++++++++++++++++++++ 4 files changed, 173 insertions(+) create mode 100644 Problem-1.java create mode 100644 Problem-1.py create mode 100644 Problem-2.java create mode 100644 Problem-2.py diff --git a/Problem-1.java b/Problem-1.java new file mode 100644 index 00000000..aaf27cf5 --- /dev/null +++ b/Problem-1.java @@ -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> levelOrder(TreeNode root) { + List> result = new ArrayList<>(); + if (root == null) return result; + + Queue queue = new LinkedList<>(); + queue.add(root); + + while (!queue.isEmpty()) { + int n = queue.size(); + List 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; + } +} diff --git a/Problem-1.py b/Problem-1.py new file mode 100644 index 00000000..4a1a320c --- /dev/null +++ b/Problem-1.py @@ -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 + + + + \ No newline at end of file diff --git a/Problem-2.java b/Problem-2.java new file mode 100644 index 00000000..2b6eef97 --- /dev/null +++ b/Problem-2.java @@ -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> 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 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; + } +} diff --git a/Problem-2.py b/Problem-2.py new file mode 100644 index 00000000..14754d4e --- /dev/null +++ b/Problem-2.py @@ -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)