Skip to content

Instantly share code, notes, and snippets.

@vbrazo
Created October 1, 2019 22:56
Show Gist options
  • Save vbrazo/61fb8e78cfd4040eb0677e3af76f284b to your computer and use it in GitHub Desktop.
Save vbrazo/61fb8e78cfd4040eb0677e3af76f284b to your computer and use it in GitHub Desktop.

Revisions

  1. vbrazo created this gist Oct 1, 2019.
    41 changes: 41 additions & 0 deletions zigzap.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    # =begin
    # # Given a Balanced Binary Tree (BBT)
    # # (non-sorted, non-search, containing unique elements)

    # 42
    # / \
    # / \
    # / \
    # / \
    # 81 99
    # / \ / \
    # 17 1 25 19
    # / \ / \ / \ / \
    # 8 -1 2 9 5 6 3 31

    # and its array representation:
    # [42, 81, 99, 17, 1, 25, 19, 8, -1, 2, 9, 5, 6, 3, 31]


    # Write a function that will take any BBT array of integers
    # (structured in the manner above), and an integer in that array,
    # and return a list of "zigzag" directions to get to that destination,
    # starting from the root.

    # To go left, adds "zig"
    # To go right, adds "zag"

    # Example:

    # tree = [42, 81, 99, 17, 1, 25, 19, 8, -1, 2, 9, 5, 6, 3, 31]

    # zigzag_directions(tree, 42) -> []
    # zigzag_directions(tree, 31) -> ['zag', 'zag', 'zag']
    # zigzag_directions(tree, -1) -> ['zig', 'zig', 'zag']
    # zigzag_directions(tree, 100) -> None


    # zigzag_directions(tree, 1) -> ['zig', 'zag']
    # zigzag_directions(tree, 6) -> ['zag', 'zig', 'zag']

    # =end