Imagine you are standing on the right side of a binary tree. Your task is to return a list containing the values of the nodes you can see, ordered from top to bottom.
Details:
root
of a binary tree.Example 1:
Consider the following binary tree:
1
/ \
2 3
\ \
5 4
In this case, the right side view would be [1, 3, 4]
. You can see node 1 at the top level, node 3 at the second level, and node 4 at the third level.
Example 2:
Consider the following binary tree:
1
/ \
2 3
/
4
/
5
In this case, the right side view would be [1, 3, 4, 5]
. Notice how 2 is blocked by 3. 4 is blocked by the implicit null node after 3 at level 2 and finally we see 5 at level 3.
Constraints:
[0, 100]
. Ensure your solution handles empty trees correctly.Can you describe an algorithm to efficiently find the right side view of a binary tree?
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine shining a light from the right side of the tree. The brute force method involves checking every node at each level to see if it's the first one you'd see from the right. We accomplish this by systematically exploring all possible paths in the tree level by level.
Here's how the algorithm would work step-by-step:
def right_side_view_brute_force(root):
if not root:
return []
right_view = []
nodes_at_level = [root]
while nodes_at_level:
level_size = len(nodes_at_level)
rightmost_node_found = False
# Iterate through the current level in reverse order
for index in range(level_size - 1, -1, -1):
current_node = nodes_at_level[index]
# Check if we have already found the rightmost node
if not rightmost_node_found:
right_view.append(current_node.val)
# Mark that we have found the rightmost node
rightmost_node_found = True
next_level_nodes = []
# Add children for the next level's processing
for node in nodes_at_level:
if node.left:
# Add left node
next_level_nodes.append(node.left)
if node.right:
# Add right node
next_level_nodes.append(node.right)
nodes_at_level = next_level_nodes
return right_view
To find the rightmost node at each level of a binary tree, we'll use a level-by-level exploration strategy. This approach ensures we always discover the rightmost node before any other node on the same level, giving us the 'right side view'.
Here's how the algorithm would work step-by-step:
from collections import deque
def binary_tree_right_side_view(root_node):
right_side_view = []
if not root_node:
return right_side_view
node_queue = deque([root_node])
while node_queue:
level_length = len(node_queue)
# Iterate through all nodes at current level.
for index in range(level_length):
current_node = node_queue.popleft()
# The last node processed is rightmost.
if index == level_length - 1:
right_side_view.append(current_node.val)
# Add left child to queue for next level.
if current_node.left:
node_queue.append(current_node.left)
# Add right child to queue for next level.
if current_node.right:
node_queue.append(current_node.right)
return right_side_view
Case | How to Handle |
---|---|
Null or empty tree | Return an empty list immediately because there are no nodes to view from the right side. |
Tree with only a root node | Return a list containing only the root node's value because it is the only visible node. |
Completely skewed tree (left or right) | The algorithm should correctly identify the rightmost node at each level even in skewed trees; breadth-first search will handle it. |
Perfectly balanced tree | The algorithm will efficiently process a balanced tree, correctly identifying the rightmost node at each level. |
Tree with duplicate values | Duplicate values do not affect the algorithm's logic as it focuses on the rightmost node at each level, regardless of its value. |
Tree with negative values | The algorithm should work correctly with negative node values, as value comparisons aren't the primary logic. |
Large tree (scalability) | Using a level-order traversal (BFS) provides a scalable solution with O(N) time complexity where N is the number of nodes. |
Tree with a wide variance in level depths | Breadth-first search efficiently handles varied level depths ensuring each rightmost node is correctly captured. |