Add implementation for leetcode problems 199, 993#1595
Add implementation for leetcode problems 199, 993#1595rishigoswamy wants to merge 1 commit intosuper30admin:masterfrom
Conversation
Right Side View of a tree (leetcode_199.py)Note: The verdict should be based on whether the student's solution is correct and efficient enough to pass the problem constraints. IMPORTANT: The student has provided multiple implementations in their solution. You must evaluate the first one (the one that is not commented out) as the primary solution. The other commented implementations are alternatives that the student is considering, but you should focus on the first one. Let's begin. VERDICT: NEEDS_IMPROVEMENT Cousins in Tree (leetcode_993.py)Your approach of using BFS to traverse the tree level by level is correct in theory. However, there is a significant issue in your implementation: you do not reset the To fix this, you should reset these variables at the start of each level. Alternatively, you can structure your code like the reference solution: for each level, you process all nodes and check if either Another issue is that you are only checking the children of the current node for But in the while loop, you are processing nodes and then only looking at their children. This means that the current node's value is never checked. However, the problem states that the root is at depth 0. So if However, for other nodes, you are only checking the children. So if In your code, you are only detecting But wait: when you add a node to the queue, you are not storing any parent information for that node. So when you later pop it, you don't know who its parent is. However, you only need the parent when you detect The main flaw is the lack of resetting the flags per level. You should move the declarations of Also, you should note that the reference solution uses a separate queue to store the parent of each node. In your code, you are not storing the parent for each node in the queue. Instead, you are only recording the parent when you check the children. This is sufficient because you only need the parent when you find But to fix your code, you need to reset the flags for each level. Here is how you can modify your code: while queue:
foundX = False # Reset for this level
foundY = False
parentX = None
parentY = None
size = len(queue)
for i in range(size):
node = queue.popleft()
if node.left:
if node.left.val == x:
foundX = True
parentX = node
if node.left.val == y:
foundY = True
parentY = node
queue.append(node.left)
if node.right:
if node.right.val == x:
foundX = True
parentX = node
if node.right.val == y:
foundY = True
parentY = node
queue.append(node.right)
if foundX and foundY:
return parentX != parentY
if foundX or foundY:
return False
return FalseThis way, at the start of each level, the flags are reset. Then, after processing all nodes in the level, you check if both were found. If so, return whether parents are different. If only one was found, return false. Also, note that the initial check for the root being x or y is correct. But what if both x and y are the root? Actually, the problem states x != y and they exist. So if the root is x, then y is not the root. So the initial check is sufficient. Another corner case: if x and y are the same level but same parent? Then they are siblings, VERDICT: NEEDS_IMPROVEMENT |
No description provided.