-
Notifications
You must be signed in to change notification settings - Fork 14
/
108-convert-sorted-array-to-binary-search-tree.py
52 lines (40 loc) · 1.48 KB
/
108-convert-sorted-array-to-binary-search-tree.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:
n = len(nums)
def build_helper(left, right):
if left > right:
return None
mid = (left + right) // 2
node = TreeNode(nums[mid])
node.left = build_helper(left, mid - 1)
node.right = build_helper(mid + 1, right)
return node
return build_helper(0, n - 1)
# time O(n), due to traverse each node once
# space O(logn), due to memo stack's size, and output is O(n)
# using tree and divide and conquer and re-build BST (top-down approach)
class Solution:
def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:
idx = 0
def dfs(left, right):
nonlocal idx
if left > right:
return None
mid = (left + right) // 2
left_subtree = dfs(left, mid - 1)
node = TreeNode(nums[idx])
idx += 1
node.left = left_subtree
right_subtree = dfs(mid + 1, right)
node.right = right_subtree
return node
return dfs(0, len(nums) - 1)
# time O(n)
# space O(logn), due to recursion stack
# using tree and divide and conquer and re-build BST (inorder approach)