From 81230f6a3927e1355ad0e9a0585d96b27859d1d9 Mon Sep 17 00:00:00 2001 From: Tyrah Date: Thu, 12 Jan 2023 13:05:46 -0800 Subject: [PATCH] passed all test cases --- binary_search_trees/array_to_bst.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/binary_search_trees/array_to_bst.py b/binary_search_trees/array_to_bst.py index f69cc42..88ef75b 100644 --- a/binary_search_trees/array_to_bst.py +++ b/binary_search_trees/array_to_bst.py @@ -6,8 +6,15 @@ def __init__(self, value, left = None, right = 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 + if not arr: + return None + + # make middle value the root + mid = (len(arr)) // 2 + + root = TreeNode(arr[mid]) + + root.left = arr_to_bst(arr[:mid]) + + root.right = arr_to_bst(arr[mid + 1:]) + return root \ No newline at end of file