Sitemap
Mergesuccessful

Mergesuccessful

On a journey to be a leetcode knight

A simple question? We can find the length of an amoeba or a brick? BRICK!!(So calculate size Between while and for)
This simple fact made my head spin today when i misplaced the size of the queue in the for loop! To tackle this problem I put root in queue, thereon adding the left and right child for every element of queue.Standard BST. Is it!! NO!!!

Get Mergesuccessful’s stories in your inbox

Join Medium for free to get updates from this writer.

I calculated the size inside the for loop, not paying attention that this will recalculate the size of queue for every element in queue.
Once that was spotted (AFTER 2 HOURS and using AI tool) I corrected it and got the solution

# 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 rightSideView(self, root: Optional[TreeNode]) -> List[int]:
if not root:
return []
q, res = [],[]
q.append(root)
while q:
# tmp = []
size = len(q)
for index in range(size):
print(index, len(q))

node = q.pop(0)
if index == size-1:
print(f'here{index} -> {node.val}')
res.append(node.val)
if node.left:
q.append(node.left)
if node.right:
q.append(node.right)
# res.append(tmp)
print(res)
return res
Press enter or click to view image in full size

Back to fight leetcode harder than ever! More solutions to follow:)

--

--

Mergesuccessful
Mergesuccessful

Written by Mergesuccessful

0 followers

On a journey to be a leetcode knight