Create bst_convert_sorted_array.py

This commit is contained in:
marina 2023-08-03 16:05:01 -07:00 committed by GitHub
parent 072febeb9c
commit eb7a7af574
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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)