MCPcopy Create free account
hub / github.com/HuberTRoy/leetCode / Solution

Class Solution

Tree/ConvertSortedArrayToBinarySearchTree.py:53–77  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

51# self.right = None
52
53class Solution(object):
54 def sortedArrayToBST(self, nums):
55 """
56 :type nums: List[int]
57 :rtype: TreeNode
58 """
59 if not nums:
60 return None
61
62 def makeBinarySearchTree(nums):
63 mid = len(nums) // 2
64
65 root = TreeNode(nums[mid])
66 left = nums[:mid]
67 right = nums[mid+1:]
68
69 if left:
70 root.left = makeBinarySearchTree(left)
71 if right:
72 root.right = makeBinarySearchTree(right)
73
74 return root
75
76 root = makeBinarySearchTree(nums)
77 return root
78

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected