forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 14
/
balance-a-binary-search-tree.py
83 lines (74 loc) · 2.41 KB
/
balance-a-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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# Time: O(n)
# Space: O(h)
# Definition for a binary tree node.
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
# dfs solution with stack
class Solution(object):
def balanceBST(self, root):
"""
:type root: TreeNode
:rtype: TreeNode
"""
def inorderTraversal(root):
result, stk = [], [(root, False)]
while stk:
node, is_visited = stk.pop()
if node is None:
continue
if is_visited:
result.append(node.val)
else:
stk.append((node.right, False))
stk.append((node, True))
stk.append((node.left, False))
return result
def sortedArrayToBst(arr):
ROOT, LEFT, RIGHT = range(3)
result = [None]
stk = [(0, len(arr), ROOT, result)]
while stk:
i, j, update, ret = stk.pop()
if i >= j:
continue
mid = i + (j-i)//2
node = TreeNode(arr[mid])
if update == ROOT:
ret[0] = node
elif update == LEFT:
ret[0].left = node
else:
ret[0].right = node
stk.append((mid+1, j, RIGHT, [node]))
stk.append((i, mid, LEFT, [node]))
return result[0]
return sortedArrayToBst(inorderTraversal(root))
# Time: O(n)
# Space: O(h)
# dfs solution with recursion
class Solution2(object):
def balanceBST(self, root):
"""
:type root: TreeNode
:rtype: TreeNode
"""
def inorderTraversalHelper(node, arr):
if not node:
return
inorderTraversalHelper(node.left, arr)
arr.append(node.val)
inorderTraversalHelper(node.right, arr)
def sortedArrayToBstHelper(arr, i, j):
if i >= j:
return None
mid = i + (j-i)//2
node = TreeNode(arr[mid])
node.left = sortedArrayToBstHelper(arr, i, mid)
node.right = sortedArrayToBstHelper(arr, mid+1, j)
return node
arr = []
inorderTraversalHelper(root, arr)
return sortedArrayToBstHelper(arr, 0, len(arr))