From eb7a7af574a9a32e69fb03ba66a14c90181fcc43 Mon Sep 17 00:00:00 2001 From: marina <138340846+bt3gl-cryptographer@users.noreply.github.com> Date: Thu, 3 Aug 2023 16:05:01 -0700 Subject: [PATCH] Create bst_convert_sorted_array.py --- trees/bst_convert_sorted_array.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 trees/bst_convert_sorted_array.py diff --git a/trees/bst_convert_sorted_array.py b/trees/bst_convert_sorted_array.py new file mode 100644 index 0000000..9641319 --- /dev/null +++ b/trees/bst_convert_sorted_array.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# author: bt3gl + +# note that there is no unique solution, and different choices +# for the root node would reflect on p is defined + + +def convert_sorted_array_bst(nums): + + def helper(left, right): + + if left > right: + return None + + p = (left + right) // 2 + + root = Node(nums[p]) + root.left = helper(left, p - 1) + root.right = helper(p + 1, right) + + return root + + return helper(0, len(nums) - 1) +