Challenge - 5 Problems
Right Side View Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Right Side View for a Simple Binary Tree
What is the output of the following code that prints the right side view of a binary tree?
DSA Python
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]) rightside = [] 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: rightside.append(node.val) return rightside # Tree structure: # 1 # / \ # 2 3 # \ \ # 5 4 root = TreeNode(1) root.left = TreeNode(2) root.right = TreeNode(3) root.left.right = TreeNode(5) root.right.right = TreeNode(4) print(rightSideView(root))
Attempts:
2 left
💡 Hint
Think about which nodes are visible when looking from the right side level by level.
✗ Incorrect
The right side view includes the last node at each level when traversed from left to right. At level 1: 1, level 2: 3, level 3: 4.
🧠 Conceptual
intermediateUnderstanding the Right Side View Concept
Which of the following best describes the right side view of a binary tree?
Attempts:
2 left
💡 Hint
Think about which nodes you see if you stand on the right side of the tree.
✗ Incorrect
The right side view shows the nodes that are the last visible nodes at each level when looking from the right side.
🔧 Debug
advancedIdentify the Error in Right Side View Implementation
What error will the following code produce when trying to get the right side view of a binary tree?
DSA Python
def rightSideView(root): if root is None: return [] queue = [root] rightside = [] 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: rightside.append(node.val) return rightside
Attempts:
2 left
💡 Hint
Consider how popping from the end affects the order of nodes processed.
✗ Incorrect
Using list.pop() removes the last element, which reverses the intended level order traversal, causing the wrong nodes to be processed and an incorrect right side view.
❓ Predict Output
advancedOutput of Right Side View for a Left-Skewed Tree
What is the output of the right side view function for this left-skewed binary tree?
DSA Python
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]) rightside = [] 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: rightside.append(node.val) return rightside # Tree structure: # 1 # / # 2 # / # 3 root = TreeNode(1) root.left = TreeNode(2) root.left.left = TreeNode(3) print(rightSideView(root))
Attempts:
2 left
💡 Hint
Even if the tree leans left, the right side view shows the last node at each level.
✗ Incorrect
Each level has only one node, so the right side view includes all nodes: 1 at level 1, 2 at level 2, and 3 at level 3.
🚀 Application
expertNumber of Nodes in Right Side View for a Complete Binary Tree
Given a complete binary tree with height h (root at height 1), how many nodes will the right side view contain?
Attempts:
2 left
💡 Hint
The right side view shows exactly one node per level.
✗ Incorrect
The right side view includes exactly one node from each level, so the count equals the height h.
