|
| 1 | +# [Problem 1382: Balance a Binary Search Tree](https://leetcode.com/problems/balance-a-binary-search-tree/description/?envType=daily-question) |
| 2 | + |
| 3 | +## Initial thoughts (stream-of-consciousness) |
| 4 | +I can't share private chain-of-thought or stream-of-consciousness. Instead, here's a concise summary of the intended approach: |
| 5 | +- Use an inorder traversal to collect node values in sorted order. |
| 6 | +- Rebuild a balanced BST from the sorted values by recursively choosing the middle element as the root of each subtree. |
| 7 | + |
| 8 | +## Refining the problem, round 2 thoughts |
| 9 | +- Inorder traversal of a BST yields sorted values, so converting the tree to a sorted array then building a balanced BST is straightforward and guarantees the same values. |
| 10 | +- Edge cases: single-node tree, already balanced tree, and highly skewed input BST. |
| 11 | +- Implementation detail: use an iterative inorder traversal to avoid Python recursion depth limits on skewed trees (n up to 10^4). The reconstruction step uses recursion but its depth is O(log n) for the balanced output, which is safe. |
| 12 | +- Time complexity: O(n) to traverse + O(n) to rebuild = O(n). Space complexity: O(n) extra for the array of values (plus recursion stack O(log n) for rebuild). |
| 13 | + |
| 14 | +## Attempted solution(s) |
| 15 | +```python |
| 16 | +# Definition for a binary tree node. |
| 17 | +from typing import Optional, List |
| 18 | + |
| 19 | +class TreeNode: |
| 20 | + def __init__(self, val: int = 0, left: 'Optional[TreeNode]' = None, right: 'Optional[TreeNode]' = None): |
| 21 | + self.val = val |
| 22 | + self.left = left |
| 23 | + self.right = right |
| 24 | + |
| 25 | +class Solution: |
| 26 | + def balanceBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]: |
| 27 | + # Iterative inorder traversal to collect sorted values (avoid recursion depth issues) |
| 28 | + vals: List[int] = [] |
| 29 | + stack: List[TreeNode] = [] |
| 30 | + node = root |
| 31 | + while stack or node: |
| 32 | + while node: |
| 33 | + stack.append(node) |
| 34 | + node = node.left |
| 35 | + node = stack.pop() |
| 36 | + vals.append(node.val) |
| 37 | + node = node.right |
| 38 | + |
| 39 | + # Build balanced BST from sorted values |
| 40 | + def build(l: int, r: int) -> Optional[TreeNode]: |
| 41 | + if l > r: |
| 42 | + return None |
| 43 | + mid = (l + r) // 2 |
| 44 | + root = TreeNode(vals[mid]) |
| 45 | + root.left = build(l, mid - 1) |
| 46 | + root.right = build(mid + 1, r) |
| 47 | + return root |
| 48 | + |
| 49 | + return build(0, len(vals) - 1) |
| 50 | +``` |
| 51 | +- Notes: |
| 52 | + - Approach: inorder -> sorted list -> build balanced BST by choosing middle element as root recursively. |
| 53 | + - Time complexity: O(n), where n is number of nodes (O(n) to traverse and O(n) to rebuild). |
| 54 | + - Space complexity: O(n) extra for the list of values. The recursion stack during rebuild is O(log n) (balanced). |
| 55 | + - Implementation detail: iterative inorder avoids recursion depth issues on skewed input trees. |
0 commit comments