Skip to content

Only enqueue non-null children in binary tree level order traversal#91

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/fix-enqueue-non-null-children
Draft

Only enqueue non-null children in binary tree level order traversal#91
Copilot wants to merge 2 commits intomainfrom
copilot/fix-enqueue-non-null-children

Conversation

Copy link

Copilot AI commented Feb 17, 2026

The level order traversal was appending n.left and n.right unconditionally, enqueueing None values and causing an extra iteration through a full level of nulls at the end.

Changes

Added null checks before appending children to the next level:

# Before
for n in nodes:
    if n:
        level.append(n.val)
        new_nodes.append(n.left)   # appends even if None
        new_nodes.append(n.right)  # appends even if None

# After  
for n in nodes:
    if n:
        level.append(n.val)
        if n.left:
            new_nodes.append(n.left)
        if n.right:
            new_nodes.append(n.right)

Eliminates redundant iteration and reduces memory overhead.

Original prompt

Problem

Based on this review feedback from PR #86:

new_nodes appends n.left/n.right even when they are None, which can enqueue a full level of None values and add an extra iteration that does no useful work. It's more efficient to only enqueue non-null children (or to build the next level list from actual children) to avoid the redundant pass and unnecessary memory use.

Current Code

In src/tree/binary_tree_level_order_traversal.py, lines 47-52:

new_nodes = []
for n in nodes:
    if n:
        level.append(n.val)
        new_nodes.append(n.left)
        new_nodes.append(n.right)

The issue is that n.left and n.right are appended to new_nodes unconditionally (they could be None). This means new_nodes can contain None values, leading to an extra unnecessary iteration through a list of all None values on the next pass.

Required Change

Add None checks before appending children to new_nodes. Only enqueue non-null children:

new_nodes = []
for n in nodes:
    if n:
        level.append(n.val)
        if n.left:
            new_nodes.append(n.left)
        if n.right:
            new_nodes.append(n.right)

This avoids enqueueing None values, preventing redundant passes and unnecessary memory use.

This pull request was created from Copilot chat.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…values

Co-authored-by: cd155 <16947266+cd155@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix enqueue of None values in binary tree traversal Only enqueue non-null children in binary tree level order traversal Feb 17, 2026
Copilot AI requested a review from cd155 February 17, 2026 00:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments