|
| 1 | +# [1382. Balance a Binary Search Tree](https://leetcode.com/problems/balance-a-binary-search-tree) |
| 2 | + |
| 3 | +--- |
| 4 | + |
| 5 | +title: "Balancing a Binary Search Tree" |
| 6 | +summary: "A detailed explanation on how to balance a Binary Search Tree (BST) using in-order traversal and constructing a balanced BST from a sorted array." |
| 7 | +date: "2024年06月26日" |
| 8 | +modifiedDate: "2024年06月26日" |
| 9 | +tags: ["Binary Search Tree", "Algorithms", "JavaScript"] |
| 10 | +slug: "balancing-a-binary-search-tree" |
| 11 | + |
| 12 | +--- |
| 13 | + |
| 14 | +# Intuition |
| 15 | + |
| 16 | +To balance a Binary Search Tree (BST), the primary idea is to convert it into a sorted array using in-order traversal, and then build a balanced BST from that sorted array. This approach ensures that the middle element of the array becomes the root of the balanced BST, and recursively applying this logic to the subarrays results in a balanced tree. |
| 17 | + |
| 18 | +# Approach |
| 19 | + |
| 20 | +1. **In-order Traversal**: Perform an in-order traversal of the BST to retrieve the node values in a sorted order. |
| 21 | +2. **Build Balanced BST**: Use the sorted array of node values to construct a balanced BST. The middle element of the array will be the root, and this process is applied recursively to build the left and right subtrees. |
| 22 | + |
| 23 | +# Complexity |
| 24 | + |
| 25 | +- **Time complexity**: \(O(n)\), where \(n\) is the number of nodes in the BST. This is because we perform in-order traversal in \(O(n)\) time and constructing the balanced BST also takes \(O(n)\) time. |
| 26 | + |
| 27 | +- **Space complexity**: \(O(n)\) due to the storage required for the array of node values and the recursion stack during the tree construction process. |
| 28 | + |
| 29 | +# Code |
| 30 | + |
| 31 | +```javascript |
| 32 | +/** |
| 33 | + * Definition for a binary tree node. |
| 34 | + * function TreeNode(val, left, right) { |
| 35 | + * this.val = (val===undefined ? 0 : val) |
| 36 | + * this.left = (left===undefined ? null : left) |
| 37 | + * this.right = (right===undefined ? null : right) |
| 38 | + * } |
| 39 | + */ |
| 40 | +/** |
| 41 | + * @param {TreeNode} root |
| 42 | + * @return {TreeNode} |
| 43 | + */ |
| 44 | + |
| 45 | +// Helper function to perform in-order traversal |
| 46 | +function inorderTraversal(root, nodes) { |
| 47 | + if (root === null) return; |
| 48 | + inorderTraversal(root.left, nodes); |
| 49 | + nodes.push(root.val); |
| 50 | + inorderTraversal(root.right, nodes); |
| 51 | +} |
| 52 | + |
| 53 | +// Helper function to build a balanced BST from a sorted array |
| 54 | +function buildBalancedBST(sortedNodes, start, end) { |
| 55 | + if (start > end) return null; |
| 56 | + const mid = Math.floor((start + end) / 2); |
| 57 | + const node = new TreeNode(sortedNodes[mid]); |
| 58 | + node.left = buildBalancedBST(sortedNodes, start, mid - 1); |
| 59 | + node.right = buildBalancedBST(sortedNodes, mid + 1, end); |
| 60 | + return node; |
| 61 | +} |
| 62 | + |
| 63 | +// Main function to balance a BST |
| 64 | +function balanceBST(root) { |
| 65 | + const nodes = []; |
| 66 | + inorderTraversal(root, nodes); |
| 67 | + return buildBalancedBST(nodes, 0, nodes.length - 1); |
| 68 | +} |
| 69 | +``` |
0 commit comments