From 8d7b77a5b0623528e7820c51363fd4d937d40a88 Mon Sep 17 00:00:00 2001 From: Raha Date: Thu, 19 Jan 2023 14:55:18 -0600 Subject: [PATCH] all tests passed --- binary_search_trees/array_to_bst.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/binary_search_trees/array_to_bst.py b/binary_search_trees/array_to_bst.py index f69cc42..df893d1 100644 --- a/binary_search_trees/array_to_bst.py +++ b/binary_search_trees/array_to_bst.py @@ -10,4 +10,18 @@ def arr_to_bst(arr): 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 + if not arr: + return None + + mid_point = len(arr)//2 + + root = TreeNode(arr[mid_point]) + + # while there is something to the left of this point, set nodes for the left + root.left = arr_to_bst(arr[:mid_point]) + + # while there is something to the right of this point, set nodes for the right + root.right = arr_to_bst(arr[mid_point + 1:]) + + + return root \ No newline at end of file