From a01139ab43fc5654d1e2ef2a3d06c1ae1ad1df5a Mon Sep 17 00:00:00 2001 From: olive Date: Sun, 13 Nov 2022 14:38:50 -0600 Subject: [PATCH] solution --- binary_search_trees/array_to_bst.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/binary_search_trees/array_to_bst.py b/binary_search_trees/array_to_bst.py index f69cc42..1c1352d 100644 --- a/binary_search_trees/array_to_bst.py +++ b/binary_search_trees/array_to_bst.py @@ -10,4 +10,15 @@ 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 + # base case: root doesn't have left/right values + if not arr: + return None + # set root to middle element + mid = len(arr)//2 + root = TreeNode(arr[mid]) + + # set left & right nodes to remaining elements in array + root.left = arr_to_bst(arr[:mid]) + root.right = arr_to_bst(arr[mid+1:]) + + return root \ No newline at end of file