Bird
Raised Fist0
Interview Preptree-bfsmediumFacebookAmazonBloomberg

Binary Tree Right Side View

Choose your preparation mode4 modes available
▶
Steps
setup

Initialize rightside list and start DFS at root

The algorithm initializes an empty list 'rightside' to store the rightmost node values at each depth. It then calls the dfs function starting at the root node (value 1) with depth 0.

💡 Initializing the list and starting DFS sets up the structure to record visible nodes and begins the traversal from the top of the tree.
Line:rightside = [] dfs(root, 0)
💡 The traversal will explore nodes depth-first, starting at the root, and record nodes as it goes deeper.
📊
Binary Tree Right Side View - Watch the Algorithm Execute, Step by Step
Watching the traversal order and how the rightmost nodes are recorded at each level helps you understand why this approach efficiently captures the right side view without extra data structures.
Step 1/15
·Active fill★Answer cell
Current node: 1
12345
DFS Stack
1 (entered)depth=0
Current node: 1
12345
DFS Stack
1 (entered)depth=0
Result: [1]
Current node: 3
12345
DFS Stack
3 (entered)depth=11 (right_done)depth=0
Result: [1]
Current node: 3
12345
DFS Stack
3 (entered)depth=11 (right_done)depth=0
Result: [1, 3]
Current node: 4
12345
DFS Stack
4 (entered)depth=23 (right_done)depth=11 (right_done)depth=0
Result: [1, 3]
Current node: 4
12345
DFS Stack
4 (entered)depth=23 (right_done)depth=11 (right_done)depth=0
Result: [1, 3, 4]
12345
DFS Stack
4 (right_done)depth=23 (right_done)depth=11 (right_done)depth=0
Result: [1, 3, 4]
12345
DFS Stack
3 (left_done)depth=11 (right_done)depth=0
Result: [1, 3, 4]
Current node: 2
12345
DFS Stack
2 (entered)depth=11 (left_done)depth=0
Result: [1, 3, 4]
Current node: 2
12345
DFS Stack
2 (entered)depth=11 (left_done)depth=0
Result: [1, 3, 4]
Current node: 5
12345
DFS Stack
5 (entered)depth=22 (right_done)depth=11 (left_done)depth=0
Result: [1, 3, 4]
Current node: 5
12345
DFS Stack
5 (entered)depth=22 (right_done)depth=11 (left_done)depth=0
Result: [1, 3, 4]
12345
DFS Stack
5 (right_done)depth=22 (left_done)depth=11 (left_done)depth=0
Result: [1, 3, 4]
12345
DFS Stack
2 (returning)depth=11 (left_done)depth=0
Result: [1, 3, 4]
12345
Result: [1, 3, 4]
Return: [1,3,4]

Key Takeaways

✓ The first node visited at each depth during a right-first DFS is the rightmost visible node at that level.

This insight is hard to see from code alone because it depends on traversal order and depth checks, which are clearer when visualized step-by-step.

✓ Right children are always visited before left children, ensuring the right side view captures the correct nodes without extra data structures.

Plain English explanation: visiting right nodes first means the algorithm sees the rightmost nodes before any others at the same depth.

✓ Nodes visited later at the same depth (like node 2 and node 5) are not added to the result because the depth has already been recorded.

This concrete example from the trace shows why some nodes are hidden behind others from the right side perspective.

Practice

(1/5)
1. Consider the following BFS-based code snippet for checking if two nodes are cousins in a binary tree. Given the tree below and inputs x=4, y=5, what is the final return value of the function? Tree structure: 1 / \ 2 3 / \ 4 5
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 isCousins(root, x, y):
    if not root:
        return False
    queue = deque([root])
    while queue:
        size = len(queue)
        x_parent = y_parent = None
        for _ in range(size):
            node = queue.popleft()
            if node.left:
                queue.append(node.left)
                if node.left.val == x:
                    x_parent = node
                if node.left.val == y:
                    y_parent = node
            if node.right:
                queue.append(node.right)
                if node.right.val == x:
                    x_parent = node
                if node.right.val == y:
                    y_parent = node
        if x_parent and y_parent:
            return x_parent != y_parent
        if x_parent or y_parent:
            return False
    return False
easy
A. Raises an exception
B. false
C. null
D. true

Solution

  1. Step 1: Trace first level

    Queue starts with root (1). After processing, queue contains nodes 2 and 3. Neither 4 nor 5 found yet.
  2. Step 2: Trace second level

    Process nodes 2 and 3. Node 2's left child is 4 (x), so x_parent = 2. Node 3's right child is 5 (y), so y_parent = 3. Both parents found and are different.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Nodes 4 and 5 are cousins with different parents at same depth [OK]
Hint: Check parents at same BFS level to confirm cousin status [OK]
Common Mistakes:
  • Confusing siblings as cousins
  • Returning true if only one parent found
  • Not resetting parents each level
2. You are given a binary tree and need to find the shortest distance from the root node down to the nearest leaf node. Which algorithmic approach guarantees the optimal solution with minimal time complexity?
easy
A. Breadth-First Search (BFS) stopping at the first encountered leaf node
B. Dynamic Programming storing minimum depths for subtrees and combining results
C. Depth-First Search (DFS) exploring all paths to leaves and returning the minimum depth
D. Greedy approach choosing the child with the smaller value at each step

Solution

  1. Step 1: Understand the problem requirement

    The problem asks for the minimum depth to a leaf node, which means the shortest path from root to any leaf.
  2. Step 2: Identify the algorithm that finds shortest path in unweighted trees

    BFS explores nodes level by level and stops immediately when it finds the first leaf, ensuring the shortest path is found efficiently.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    BFS stops at first leaf -> minimal depth found [OK]
Hint: Shortest path in trees -> BFS with early stop [OK]
Common Mistakes:
  • Assuming DFS always finds minimum depth efficiently
  • Confusing greedy value-based choice with shortest path
  • Thinking DP is needed for simple tree traversal
3. Consider the following Python code for N-ary tree level order traversal. Given the tree root = Node(1, [Node(3, [Node(5), Node(6)]), Node(2), Node(4)]), what is the value of res after the first iteration of the outer while loop completes?
easy
A. [[1]]
B. [[1, 3, 2, 4]]
C. [[3, 2, 4]]
D. [[5, 6]]

Solution

  1. Step 1: Trace the first outer loop iteration

    Initially, current_level contains only the root node with value 1.
  2. Step 2: Collect values and enqueue children

    We pop node 1, append its value to level_nodes -> [1], and enqueue its children (3, 2, 4) into next_level.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    After first iteration, res = [[1]] as only root level processed [OK]
Hint: First level contains only root node value [OK]
Common Mistakes:
  • Confusing first level with all children nodes
4. What is the space complexity of the optimal BFS approach for computing the average of levels in a binary tree, where n is the number of nodes and w is the maximum width of the tree?
medium
A. O(log n) because the tree height bounds the queue size
B. O(n) because all nodes are stored in the queue at once
C. O(w) because the queue holds at most all nodes at the widest level
D. O(n) due to storing all node values in the result list

Solution

  1. Step 1: Identify maximum queue size during BFS

    The queue holds nodes level-by-level; max size is the maximum number of nodes at any level, i.e., width w.
  2. Step 2: Consider auxiliary space for result list

    Result list stores one average per level, at most height h, which is ≤ n but smaller than w in wide trees.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Queue size dominates space, bounded by maximum width w [OK]
Hint: Queue size = max nodes at any level = width w [OK]
Common Mistakes:
  • Assuming queue holds all nodes simultaneously (O(n))
  • Confusing height with width for queue size
  • Ignoring space for result list
5. The following code attempts to count good nodes in a binary tree using iterative DFS. Identify the line containing the subtle bug that causes incorrect counting.
medium
A. Line checking if node.val > max_val instead of >= max_val
B. Line updating max_val with max(max_val, node.val)
C. Line pushing node.right onto the stack
D. Line returning count at the end

Solution

  1. Step 1: Examine condition for counting good nodes

    The condition uses strictly greater (node.val > max_val), but problem definition includes equality (>=).
  2. Step 2: Understand impact of strict inequality

    This causes undercounting when node.val equals max_val, missing some good nodes.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Changing > to >= fixes counting [OK]
Hint: Check if condition uses >= not > for counting [OK]
Common Mistakes:
  • Using > instead of >=
  • Not updating max_val before pushing children
  • Ignoring null root case
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard