From 2531ec4393d4f82f90cac7f57607f54471b363ab Mon Sep 17 00:00:00 2001 From: camilla Date: Sun, 15 Jan 2023 23:08:25 -0800 Subject: [PATCH] Implemented arr_to_bst function --- binary_search_trees/array_to_bst.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/binary_search_trees/array_to_bst.py b/binary_search_trees/array_to_bst.py index f69cc42..d82d5d5 100644 --- a/binary_search_trees/array_to_bst.py +++ b/binary_search_trees/array_to_bst.py @@ -6,8 +6,14 @@ 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 + + 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 +