← Back to blog Amazon NG: Connect Binary Tree Next Pointers Level by Level (LC116)
Amazon

Amazon NG: Connect Binary Tree Next Pointers Level by Level (LC116)

2026-06-15

Amazon NG VO opens each round with BQ before coding, and this "connect a binary tree's next pointers level by level" was one round's coding question, matching LeetCode 116/117. Based on an oavoservice student's debrief, this post lays out the framing: for tree/graph problems, clarifying edge cases matters a lot, and at its core this problem is the combination of "visit a binary tree level by level" + "modify nodes' internal references," gauging your grasp of BFS vs DFS on top of that. At the end you'll find VO assist / VO proxy paths for anyone in a new-grad, career-switch, or SDE job search.


1. Problem Background

Dimension Details
Round Amazon NG VO coding round (BQ first, then coding)
Matches LeetCode 116 / 117
Language Your choice; Python common
Focus Level-order traversal, node reference modification, BFS/DFS trade-off, edge cases

2. Problem

Connect each node's next pointer in a binary tree to point to the next node on the same level (each level's rightmost node's next is None).

3. Framing: clarify first, then choose BFS / DFS

For tree/graph problems, clarifying edge cases matters a lot: perfect binary tree or general? Empty tree? Root only? Ask these before deciding the approach.

In one line, this problem is the combination of "visit a binary tree level by level" + "modify nodes' internal references," and on top of that it gauges your grasp of BFS vs DFS.

In the interview, write the clear, safe BFS first, then verbally add the O(1)-space optimization direction—showing mastery without the risk.

BFS solution (recommended)

from collections import deque

class Node:
    def __init__(self, val=0, left=None, right=None, next=None):
        self.val = val
        self.left = left
        self.right = right
        self.next = next

def connect(root):
    if not root:
        return root
    q = deque([root])
    while q:
        size = len(q)
        prev = None
        for _ in range(size):
            node = q.popleft()
            if prev:
                prev.next = node      # link adjacent nodes on the same level
            prev = node
            if node.left:
                q.append(node.left)
            if node.right:
                q.append(node.right)
        # prev is now the rightmost node; its next stays None
    return root

Time complexity: O(n) (each node enqueued/dequeued once). Space complexity: O(w), worst case O(n) (the queue).

O(1) space optimization (perfect binary tree)

In a perfect binary tree, once the level above is connected via next, you can traverse that level horizontally like a "linked list," stringing the next level's left/right children together without a queue:

def connect_perfect(root):
    leftmost = root
    while leftmost and leftmost.left:
        head = leftmost
        while head:
            head.left.next = head.right                 # same parent: left → right
            if head.next:
                head.right.next = head.next.left        # across parents: right → next parent's left
            head = head.next
        leftmost = leftmost.left
    return root

Time complexity: O(n). Space complexity: O(1) (excluding output).

4. On-the-Spot Reminders


FAQ

Q1: For this problem, BFS or DFS?

Level-order BFS is the most intuitive and safe, O(w) space; a perfect binary tree can use the already-connected next of the level above for O(1) extra space, but the logic is trickier. In the interview, write the safe BFS first, then verbally add the O(1) optimization to show mastery.

Q2: What's the difference between LC116 and LC117?

116 is a perfect binary tree (every level full), so the "same-parent left/right + across-parent" rule gives O(1) space directly; 117 is a general tree where children may be missing, needing extra logic to find the next level's first valid child, making the O(1) solution more involved.

Q3: Why is clarifying edge cases important?

Tree shape (perfect / general), empty tree, and root-only all affect the approach and boundaries. Asking up front avoids discovering a wrong assumption mid-solution, and shows the interviewer your rigorous engineering habits.

Q4: How do I prepare efficiently for Amazon NG coding rounds?

Drill by topic—tree/graph traversal, linked lists, DP, two pointers—timed and narrating your reasoning, while also preparing BQ. For timed mocks by Amazon NG question type plus BQ, line-by-line walkthroughs, or full VO assist / VO proxy support, see the path below.


Preparing for Amazon NG VO?

Amazon NG is a BQ + coding loop where coding loves to probe BFS/DFS trade-offs on classic tree problems. oavoservice offers full Amazon prep: timed mocks on tree / graph / linked list / DP high-frequency questions, per-question polishing of edge-case clarification and O(1) optimization, and parallel prep of STAR stories across the 16 LPs. Coaches include senior big-tech engineers with a Bar Raiser perspective, with full VO assist / VO proxy support.

Add WeChat Coding0201 now to get Amazon questions and coaching.

Contact