#199
Binary Tree Right Side View
medium· Treesruns: 0Given the root of a binary tree, imagine yourself standing on the right side of the tree. Return the values of the nodes you can see, ordered from top to bottom.
sign in to paste and practice your own solution
wpm 0acc 100%time 0:000 / 570
from collections import deque
class Solution:
def rightSideView(self, root: TreeNode | None) -> list[int]:
if not root:
return []
result = []
queue = deque([root])
while queue:
n = len(queue)
for i in range(n):
node = queue.popleft()
if i == n - 1:
result.append(node.val)
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
return result
click the box to focus · tab inserts 4 spaces · backspace to correct · esc to pause
desktop only
codedrill is a typing game and needs a real keyboard. open this on a laptop or desktop to practice.
you can still browse problems and sections from your phone.