-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLC0391.py
34 lines (28 loc) · 1.31 KB
/
LC0391.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
class Solution:
def balanceBST(self, root: TreeNode) -> TreeNode:
# Create a list to store the inorder traversal of the BST
inorder = []
self.inorder_traversal(root, inorder)
# Construct and return the balanced BST
return self.create_balanced_bst(inorder, 0, len(inorder) - 1)
def inorder_traversal(self, root: TreeNode, inorder: list):
# Perform an inorder traversal to store the elements in sorted order
if not root:
return
self.inorder_traversal(root.left, inorder)
inorder.append(root.val)
self.inorder_traversal(root.right, inorder)
def create_balanced_bst(
self, inorder: list, start: int, end: int
) -> TreeNode:
# Base case: if the start index is greater than the end index, return None
if start > end:
return None
# Find the middle element of the current range
mid = start + (end - start) // 2
# Recursively construct the left and right subtrees
left_subtree = self.create_balanced_bst(inorder, start, mid - 1)
right_subtree = self.create_balanced_bst(inorder, mid + 1, end)
# Create a new node with the middle element and attach the subtrees
node = TreeNode(inorder[mid], left_subtree, right_subtree)
return node