Skip to content

Instantly share code, notes, and snippets.

@amraks
Created February 16, 2019 18:49
Show Gist options
  • Save amraks/4b7ecd235968bd8095acaae80b2f9f57 to your computer and use it in GitHub Desktop.
Save amraks/4b7ecd235968bd8095acaae80b2f9f57 to your computer and use it in GitHub Desktop.
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
# 2 3 6 8 10 12 14 20 22 28 30
class Solution(object):
def to_bst(self, nums):
if not nums:
return
mid = len(nums)/2;
root = TreeNode(nums[mid])
root.left = self.to_bst(nums[0:mid])
root.right = self.to_bst(nums[mid+1:])
return root
def sortedArrayToBST(self, nums):
"""
:type nums: List[int]
:rtype: TreeNode
"""
if not nums:
return
return self.to_bst(nums)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment