Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 41a4ba8

Browse files
Merge pull request #54 from codewithhridoy/javascript
medium: 1382. Balance a Binary Search Tree
2 parents 243caef + 6c38bc6 commit 41a4ba8

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} root
11+
* @return {TreeNode}
12+
*/
13+
14+
// Helper function to perform in-order traversal
15+
function inorderTraversal(root, nodes) {
16+
if (root === null) return;
17+
inorderTraversal(root.left, nodes);
18+
nodes.push(root.val);
19+
inorderTraversal(root.right, nodes);
20+
}
21+
22+
// Helper function to build a balanced BST from a sorted array
23+
function buildBalancedBST(sortedNodes, start, end) {
24+
if (start > end) return null;
25+
const mid = Math.floor((start + end) / 2);
26+
const node = new TreeNode(sortedNodes[mid]);
27+
node.left = buildBalancedBST(sortedNodes, start, mid - 1);
28+
node.right = buildBalancedBST(sortedNodes, mid + 1, end);
29+
return node;
30+
}
31+
32+
// Main function to balance a BST
33+
function balanceBST(root) {
34+
const nodes = [];
35+
inorderTraversal(root, nodes);
36+
return buildBalancedBST(nodes, 0, nodes.length - 1);
37+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /