📖 Scenario: You are working on a program that helps visualize a binary tree from the right side. Imagine standing on the right side of a tree and seeing only the nodes that are visible from that angle.
🎯 Goal: Build a program that creates a binary tree, then finds and prints the list of node values visible from the right side.
📋 What You'll Learn
Create a binary tree with given nodes
Use a variable to hold the right side view nodes
Implement a function to find the right side view of the binary tree
Print the list of visible node values from the right side
💡 Why This Matters
🌍 Real World
Right side view of a binary tree helps in visualizing hierarchical data from a specific angle, useful in UI trees, organizational charts, and decision trees.
💼 Career
Understanding tree traversal and views is important for software engineers working with data structures, algorithms, and system design.
Progress0 / 4 steps
1
Create the Binary Tree Nodes
Create a class called TreeNode with an __init__ method that takes val, left, and right parameters. Then create the binary tree with root node value 1, left child 2, right child 3, left child of 2 as 4, and right child of 3 as 5. Assign the root node to a variable called root.
DSA Python
Hint
Define the TreeNode class first, then create nodes and link them as left and right children.
2
Create a List to Hold Right Side View Nodes
Create an empty list called right_view that will store the values of nodes visible from the right side.
DSA Python
Hint
Just create an empty list named right_view to store visible node values.
3
Implement Function to Find Right Side View
Define a function called rightSideView that takes root as input. Use a queue to perform level order traversal. For each level, add the last node's value to right_view. Use a while loop and for loop with variables level_length and i to process nodes level by level.
DSA Python
Hint
Use a queue to visit nodes level by level. Add the last node's value of each level to right_view.
4
Print the Right Side View List
Print the right_view list to show the node values visible from the right side.
DSA Python
Hint
Use print(right_view) to display the final list of visible nodes.
Practice
(1/5)
1. What does the "Right Side View of Binary Tree" represent?
easy
A. Nodes visible when the tree is viewed from the right side
B. Nodes visible when the tree is viewed from the left side
C. All leaf nodes of the binary tree
D. All nodes at the deepest level of the tree
Solution
Step 1: Understand the view perspective
The right side view shows nodes visible when looking at the tree from the right side.
Step 2: Differentiate from other views
Unlike left side or leaf views, this specifically shows the rightmost nodes at each level.
Final Answer:
Nodes visible when the tree is viewed from the right side -> Option A
Quick Check:
Right side view = rightmost nodes [OK]
Hint: Right side view shows rightmost nodes at each level [OK]
Common Mistakes:
Confusing right side view with left side view
Thinking it shows all leaf nodes
Assuming it shows deepest level nodes only
2. Which of the following Python code snippets correctly initializes an empty list to store the right side view nodes?
easy
A. right_view = set()
B. right_view = []
C. right_view = ()
D. right_view = {}
Solution
Step 1: Identify the correct data structure for ordered collection
We need a list to store nodes in order, so an empty list is initialized with [].
Step 2: Check other options
{} is a dictionary, () is a tuple, set() is unordered; none fit the requirement.
Final Answer:
right_view = [] -> Option B
Quick Check:
Empty list uses [] [OK]
Hint: Use [] for empty list to store nodes [OK]
Common Mistakes:
Using {} which creates a dictionary
Using () which creates an empty tuple
Using set() which is unordered
3. What is the output of the following code that prints the right side view of the binary tree?
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def rightSideView(root):
if not root:
return []
from collections import deque
queue = deque([root])
right_view = []
while queue:
level_length = len(queue)
for i in range(level_length):
node = queue.popleft()
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
if i == level_length - 1:
right_view.append(node.val)
return right_view
# Tree:
# 1
# / \
# 2 3
# \ \
# 5 4
root = TreeNode(1, TreeNode(2, None, TreeNode(5)), TreeNode(3, None, TreeNode(4)))
print(rightSideView(root))
medium
A. [1, 3, 4]
B. [1, 2, 5]
C. [1, 3, 5]
D. [1, 2, 4]
Solution
Step 1: Traverse level by level and identify rightmost nodes
Level 1: Node 1 (only node), Level 2: Nodes 2 and 3, rightmost is 3, Level 3: Nodes 5 and 4, rightmost is 4.
Step 2: Collect rightmost nodes in order
Right side view list is [1, 3, 4].
Final Answer:
[1, 3, 4] -> Option A
Quick Check:
Rightmost nodes per level = [1, 3, 4] [OK]
Hint: Pick last node at each level during BFS [OK]
Common Mistakes:
Picking leftmost node instead of rightmost
Appending nodes before checking level end
Confusing node values at each level
4. Find the error in the following code snippet for right side view and select the fix:
def rightSideView(root):
if not root:
return []
queue = [root]
right_view = []
while queue:
level_length = len(queue)
for i in range(level_length):
node = queue.pop()
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
if i == level_length - 1:
right_view.append(node.val)
return right_view
medium
A. Remove the if condition checking i == level_length - 1
B. Replace queue.append(node.left) with queue.insert(0, node.left)
C. Initialize queue as a deque instead of list
D. Replace queue.pop() with queue.pop(0) to pop from front
Solution
Step 1: Identify queue operation error
Using pop() removes last element, but BFS requires removing from front (FIFO).
Step 2: Fix by popping from front
Use pop(0) to remove first element, maintaining correct BFS order.
Final Answer:
Replace queue.pop() with queue.pop(0) to pop from front -> Option D
Quick Check:
Queue pop front for BFS [OK]
Hint: Use pop(0) to dequeue from front in list-based BFS [OK]