Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

draft list_of_depths #333

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions cracking_the_code/chapter4/list_of_depths.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import collections


def dfs_tree2list(node, lvl, arr):
if node is None:
return
if lvl >= len(arr):
linked_lst = collections.deque() # linkedlist
arr.append(linked_lst)
arr[lvl].append(node.value)
dfs_tree2list(node.left, lvl+1, arr)
dfs_tree2list(node.right, lvl+1, arr)


def dft_tree2list(node, arr):
current = collections.deque() # linkedlist
current.append(node)

while current: # while not empty
arr.append(current)
parents = current
for parent in parents:
if parent.left:
current.append(parent.left)
if parent.right:
current.append(parent.right)


def main():
# TODO: Create Tree
dfs_tree2list([], 0, [])


if __name__ == "__main__":
main()