-
Notifications
You must be signed in to change notification settings - Fork 19
/
answer.py
28 lines (24 loc) · 801 Bytes
/
answer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/env python3
#-------------------------------------------------------------------------------
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def sortedArrayToBST(self, nums):
"""
:type nums: List[int]
:rtype: TreeNode
"""
def help(left, right):
if left > right: return None
mid = (left + right) // 2
root = TreeNode(nums[mid])
root.left = help(left, mid-1)
root.right = help(mid+1, right)
return root
return help(0, len(nums)-1)
#-------------------------------------------------------------------------------
# Testing