-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1382_Balance_a_Binary_Search_Tree.java
53 lines (43 loc) · 1.58 KB
/
1382_Balance_a_Binary_Search_Tree.java
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
* 1382. Balance a Binary Search Tree
* Problem Link: https://leetcode.com/problems/balance-a-binary-search-tree/
* Difficulty: Medium
*
* Solution Created by: Muhammad Khuzaima Umair
* LeetCode : https://leetcode.com/mkhuzaima/
* Github : https://github.com/mkhuzaima
* LinkedIn : https://www.linkedin.com/in/mkhuzaima/
*/
class Solution {
public TreeNode balanceBST(TreeNode root) {
// Create a list to store the nodes in inorder traversal order
List<TreeNode> list = new ArrayList<>();
inorderTraversal(list, root);
// Build a new balanced BST from the list of nodes
return buildBalancedBST(list, 0, list.size() - 1);
}
private void inorderTraversal(List<TreeNode> list, TreeNode node) {
if (node == null) {
return;
}
// Recursively traverse the left subtree
inorderTraversal(list, node.left);
// Add the current node to the list
list.add(node);
// Recursively traverse the right subtree
inorderTraversal(list, node.right);
}
private TreeNode buildBalancedBST(List<TreeNode> list, int start, int end) {
// Base case: no more nodes to add to the subtree
if (start > end) {
return null;
}
// Select the middle element as the root node
int mid = (start + end) / 2;
TreeNode root = list.get(mid);
// Recursively build the left and right subtrees
root.left = buildBalancedBST(list, start, mid - 1);
root.right = buildBalancedBST(list, mid + 1, end);
return root;
}
}