diff --git a/binary_search_trees/array_to_bst.py b/binary_search_trees/array_to_bst.py index f69cc42..8a6c4df 100644 --- a/binary_search_trees/array_to_bst.py +++ b/binary_search_trees/array_to_bst.py @@ -4,10 +4,23 @@ def __init__(self, value, left = None, right = None): self.left = left self.right = right +def bst_helper(arr): + if arr: + this_node_index = len(arr) // 2 + this_node_val = arr[this_node_index] + left_array = arr[0:this_node_index] + right_array = arr[(this_node_index + 1):] + left_node = bst_helper(left_array) + right_node = bst_helper(right_array) + + result = TreeNode(this_node_val, left_node, right_node) + return result + else: + return None def arr_to_bst(arr): """ Given a sorted array, write a function to create a Balanced Binary Search Tree using the elements in the array. Return the root of the Binary Search Tree. """ - pass \ No newline at end of file + return bst_helper(arr) diff --git a/requirements.txt b/requirements.txt index 77ffa3f..19891d7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,8 +1,8 @@ -attrs==20.3.0 -iniconfig==1.1.1 -packaging==20.8 -pluggy==0.13.1 -py==1.10.0 -pyparsing==2.4.7 -pytest==6.2.1 -toml==0.10.2 +attrs +iniconfig +packaging +pluggy +py +pyparsinG +pytest +toml