Created
February 16, 2019 18:49
-
-
Save amraks/4b7ecd235968bd8095acaae80b2f9f57 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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