From 64a471b447b5a0304f079fd3ad7faeef6060aa2f Mon Sep 17 00:00:00 2001 From: Diana Date: Mon, 16 Jan 2023 10:27:01 -0800 Subject: [PATCH] Added function to create BST --- binary_search_trees/array_to_bst.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/binary_search_trees/array_to_bst.py b/binary_search_trees/array_to_bst.py index f69cc42..c1f8de9 100644 --- a/binary_search_trees/array_to_bst.py +++ b/binary_search_trees/array_to_bst.py @@ -10,4 +10,14 @@ 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 = (len(arr)) // 2 + + root = TreeNode(arr[mid]) + + root.left = arr_to_bst(arr[:mid]) + + root.right = arr_to_bst(arr[mid+1:]) + return root