Add implementation for leetcode problems 102, 207#1636
Add implementation for leetcode problems 102, 207#1636rishigoswamy wants to merge 1 commit intosuper30admin:masterfrom
Conversation
Level Order Traversal in Binary tree (leetcode_102.py)Your exploration of multiple approaches (DFS with level tracking, BFS with queue) is commendable. However, the active DFS implementation has a few issues:
Alternatively, the BFS solution you provided (commented out) is correct and efficient. I recommend using that because it is more straightforward for level-order traversal. Here's a cleaned-up version: from collections import deque
from typing import List, Optional
class Solution:
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
if not root:
return []
queue = deque([root])
result = []
while queue:
level = []
for _ in range(len(queue)):
node = queue.popleft()
level.append(node.val)
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
result.append(level)
return resultThis BFS approach is standard and avoids recursion, which can be easier to understand. VERDICT: NEEDS_IMPROVEMENT Scheduling Courses (leetcode_207.py)Your solution is well-written and correctly implements Kahn's algorithm for topological sorting. Here are some points to consider for improvement:
Example modification: count = 0
while queue:
currCourse = queue.popleft()
count += 1
for childCourse in adjMap[currCourse]:
indegreeCount[childCourse] -= 1
if indegreeCount[childCourse] == 0:
queue.append(childCourse)
return count == numCoursesThis change would make the code slightly more efficient and concise. Overall, your solution is correct and efficient. Good job! VERDICT: PASS |
No description provided.