Skip to content

Binary Tree Right Side View

Given the root of a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.

InputOutputExplanation
[1,2,3,null,5,null,4][1,3,4]Right view: root 1, right child 3, right-most at level 3 is 4
[1,null,3][1,3]Only right children visible
[][]Empty tree
Example 1: 1 Output: [1, 3, 4]
/ \
2 3 Right side view
\ \
5 4
Example 2: 1
\
3 Output: [1, 3]
Example 3: (empty) Output: []
  • The number of nodes in the tree is in the range [0, 100].
  • -100 <= Node.val <= 100
ApproachTimeSpaceBest When
BFS Level-OrderO(n)O(w)Want to process level-by-level clearly
DFS Right-FirstO(n)O(h)Prefer simpler recursion; smaller trees

Where h = height, w = max width

  • Pick BFS Level-Order if you want a clear, intuitive level-by-level approach.
  • Pick DFS Right-First if you prefer elegant recursion that processes the right side first.
Level-by-Level
BFS Level-Order
O(n) time · O(w) space
Right-First
DFS Right-First
O(n) time · O(h) space
★ Recommended

Process the tree level by level. For each level, take the rightmost node (the last one dequeued in that level).

⏱ Time O(n) Visit all nodes once 💾 Space O(w) Queue width at widest level

Input: root = [1,2,3,null,5,null,4]

Level 1: [1] → right-most = 1
Level 2: [2, 3] → right-most = 3
Level 3: [5, 4] → right-most = 4
Result: [1, 3, 4]
function rightSideViewBFS(root):
if root is null:
return []
result = []
queue = [root]
while queue is not empty:
levelSize = length of queue
// Process all nodes at this level
for i = 0 to levelSize - 1:
node = queue.dequeue()
// Store the rightmost (last) node at this level
if i == levelSize - 1:
result.append(node.val)
if node.left exists:
queue.enqueue(node.left)
if node.right exists:
queue.enqueue(node.right)
return result
binary_tree_right_side_view_bfs.py
from typing import Optional, List
from collections import deque
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def rightSideView(root: Optional[TreeNode]) -> List[int]:
"""
Return the right side view of a binary tree using BFS.
Time Complexity: O(n) where n is the number of nodes
Space Complexity: O(w) where w is the maximum width
"""
if not root:
return []
result = []
queue = deque([root])
while queue:
level_size = len(queue)
for i in range(level_size):
node = queue.popleft()
# Record the rightmost (last) node at this level
if i == level_size - 1:
result.append(node.val)
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
return result
# Test cases
if __name__ == "__main__":
# Example 1: [1,2,3,null,5,null,4]
# 1
# / \
# 2 3
# \ \
# 5 4
root1 = TreeNode(1)
root1.left = TreeNode(2)
root1.right = TreeNode(3)
root1.left.right = TreeNode(5)
root1.right.right = TreeNode(4)
result1 = rightSideView(root1)
print(f"Right side view of tree 1: {result1}") # Expected: [1, 3, 4]
# Example 2: [1,null,3]
# 1
# \
# 3
root2 = TreeNode(1)
root2.right = TreeNode(3)
result2 = rightSideView(root2)
print(f"Right side view of tree 2: {result2}") # Expected: [1, 3]
# Example 3: Empty tree
root3 = None
result3 = rightSideView(root3)
print(f"Right side view of tree 3: {result3}") # Expected: []
🎯 Interview Favourite

Use DFS and visit the right child before the left child. Track the current level. Record a node only the first time we visit its level (which is the rightmost at that level).

⏱ Time O(n) Visit all nodes 💾 Space O(h) Recursion call stack

Input: root = [1,2,3,null,5,null,4]

DFS order (right-first): 1 (level 0) → 3 (level 1) → 4 (level 2) → 2 (level 1, skip) → 5 (level 2, skip)

Result: [1, 3, 4]

function rightSideViewDFS(root):
result = []
function dfs(node, level):
if node is null:
return
// First time reaching this level, it's the rightmost
if level == length of result:
result.append(node.val)
// Right child first, then left
dfs(node.right, level + 1)
dfs(node.left, level + 1)
dfs(root, 0)
return result
binary_tree_right_side_view_dfs.py
from typing import Optional, List
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def rightSideView(root: Optional[TreeNode]) -> List[int]:
"""
Return the right side view of a binary tree using DFS (right-first).
Time Complexity: O(n) where n is the number of nodes
Space Complexity: O(h) where h is the height (call stack depth)
"""
result = []
def dfs(node: Optional[TreeNode], level: int) -> None:
if not node:
return
# First time visiting this level, it's the rightmost
if level == len(result):
result.append(node.val)
# Visit right subtree first, then left
dfs(node.right, level + 1)
dfs(node.left, level + 1)
dfs(root, 0)
return result
# Test cases
if __name__ == "__main__":
# Example 1: [1,2,3,null,5,null,4]
# 1
# / \
# 2 3
# \ \
# 5 4
root1 = TreeNode(1)
root1.left = TreeNode(2)
root1.right = TreeNode(3)
root1.left.right = TreeNode(5)
root1.right.right = TreeNode(4)
result1 = rightSideView(root1)
print(f"Right side view of tree 1: {result1}") # Expected: [1, 3, 4]
# Example 2: [1,null,3]
# 1
# \
# 3
root2 = TreeNode(1)
root2.right = TreeNode(3)
result2 = rightSideView(root2)
print(f"Right side view of tree 2: {result2}") # Expected: [1, 3]
# Example 3: Empty tree
root3 = None
result3 = rightSideView(root3)
print(f"Right side view of tree 3: {result3}") # Expected: []