Skip to content

Making binary tree traversals lazy. #8725

@amirsoroush

Description

@amirsoroush

Feature description

Hi,
In binary_tree_traversals.py file, there are different kinds of traversals such as preorder, inorder, postorder and etc.

Although the implementations are pretty clean one-liner like:

# preorder
return [root.data, *preorder(root.left), *preorder(root.right)] if root else []

It isn't memory friendly. We can use generators instead not to load all the nodes into the memory:

# preorder
    if not root:
        return []
    yield root.data
    yield from preorder(root.left)
    yield from preorder(root.right)

Shall we go ahead and change them?

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementThis PR modified some existing files

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions