Skip to content

BFS 1 solution#1646

Open
indrasena483 wants to merge 1 commit into
super30admin:masterfrom
indrasena483:master
Open

BFS 1 solution#1646
indrasena483 wants to merge 1 commit into
super30admin:masterfrom
indrasena483:master

Conversation

@indrasena483
Copy link
Copy Markdown

No description provided.

@super30admin
Copy link
Copy Markdown
Owner

Level Order Traversal in Binary tree (Problem1.java)

Strengths:

  • Correct use of DFS with level tracking to achieve level-order traversal
  • Good variable naming and code organization
  • Comprehensive comments explaining time/space complexity
  • Proper handling of edge cases
  • Efficient use of data structures (LinkedList for O(1) insertion at beginning)

Areas for Improvement:

  • Space complexity analysis is incomplete. While recursion stack is O(h), the result list stores all n nodes, making total space O(n)
  • The BFS approach using a queue is more conventional for level-order traversal as it naturally processes one level completely before moving to the next
  • Consider adding comments explaining why the left subtree is processed before adding the current node's value (this ensures correct level ordering)

VERDICT: PASS


Scheduling Courses (Problem2.java)

Strengths:

  1. Correctly implements topological sort using BFS (Kahn's algorithm)
  2. Good use of HashMap and ArrayList for adjacency list
  3. Proper handling of edge cases (empty prerequisites, all zero indegrees)
  4. Clear comments explaining time and space complexity
  5. Efficient O(V + E) solution

Areas for Improvement:

  1. The initial counting logic is slightly confusing. Consider simplifying: instead of decrementing cnt for zero-indegree nodes upfront, just check if the count of zero-indegree nodes equals numCourses and return true immediately.
  2. The variable name cnt could be more descriptive (e.g., coursesCompleted or remainingCourses).
  3. The early return if (cnt == 0) return true; after adding all zero-indegree nodes could be combined with the final check for cleaner code.

VERDICT: PASS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants