-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy path1382-kir3i.py
More file actions
30 lines (27 loc) · 866 Bytes
/
1382-kir3i.py
File metadata and controls
30 lines (27 loc) · 866 Bytes
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
# 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 balanceBST(self, root):
vals = []
self.popNode(root, vals)
return self.makeTree(0, len(vals)-1, vals)
def makeTree(self, lo, hi, vals):
if lo > hi:
return None
elif lo == hi:
return TreeNode(vals[lo])
mid = (lo + hi) // 2
root = TreeNode(vals[mid])
root.left = self.makeTree(lo, mid-1, vals)
root.right = self.makeTree(mid+1, hi, vals)
return root
def popNode(self, root, res):
if root.left:
self.popNode(root.left, res)
res.append(root.val)
if root.right:
self.popNode(root.right, res)