Conversation
Right Side View of a tree (leetcode-199.py)Your solution correctly implements the level-order traversal to capture the rightmost node at each level. The code is efficient with O(n) time and space complexity. However, there are a few points to improve:
Overall, your solution is correct and efficient. With minor improvements in code clarity, it will be excellent. VERDICT: PASS Cousins in Tree (leetcode-993.py)Your approach using BFS is good, and you have the right idea to track the parent and depth. However, there are a few issues to address:
Here is a corrected version of your code using tuples: from collections import deque
class Solution:
def isCousins(self, root: Optional[TreeNode], x: int, y: int) -> bool:
if root.val == x or root.val == y:
return False
q = deque()
q.append((root, None)) # (node, parent)
x_found = False
y_found = False
x_parent = None
y_parent = None
while q:
size = len(q)
for _ in range(size):
curr, parent = q.popleft()
if curr.val == x:
x_found = True
x_parent = parent
if curr.val == y:
y_found = True
y_parent = parent
if curr.left:
q.append((curr.left, curr))
if curr.right:
q.append((curr.right, curr))
if x_found and y_found:
return x_parent != y_parent
if x_found or y_found:
return False
return FalseThis code processes each level completely, then checks at the end of the level. It stores the parent node (not value), which is more accurate. VERDICT: NEEDS_IMPROVEMENT |
No description provided.