Explain right-side view.
01
01
Understand the problem
treesbfs
02
02
Attempt it yourself
Sketch your approach before reading the solution — that's what interviews test.
Stuck? AI Nudge Available
Get a conceptual hint to guide your logic without spoiling the final implementation.
03
03
Study the solution
The solution is waiting
Give it an honest attempt first — then compare your thinking with the full walkthrough.
04
04
Read the code
Level-order BFS, last per level
Run Playgroundfrom collections import deque
class TreeNode:
def __init__(self, val, left=None, right=None):
self.val, self.left, self.right = val, left, right
def right_side_view(root):
if not root:
return []
view, q = [], deque([root])
while q:
n = len(q)
for i in range(n):
node = q.popleft()
if i == n - 1: # last node of this level
view.append(node.val)
if node.left: q.append(node.left)
if node.right: q.append(node.right)
return view
# --- demo ---
root = TreeNode(1, TreeNode(2, None, TreeNode(5)), TreeNode(3, None, TreeNode(4)))
print(right_side_view(root)) # [1, 3, 4]05
05
Join the discussion
Discussion (0)
Sign in to join the discussion.
No responses yet. Be the first to share what you think.