-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBalanceABinarySearchTree.java
42 lines (33 loc) · 1.05 KB
/
BalanceABinarySearchTree.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
package com.smlnskgmail.jaman.leetcodejava.medium;
import com.smlnskgmail.jaman.leetcodejava.support.TreeNode;
import java.util.ArrayList;
import java.util.List;
// https://leetcode.com/problems/balance-a-binary-search-tree/
public class BalanceABinarySearchTree {
private final TreeNode input;
public BalanceABinarySearchTree(TreeNode input) {
this.input = input;
}
public TreeNode solution() {
List<Integer> nums = new ArrayList<>();
dfs(input, nums);
return build(nums, 0, nums.size() - 1);
}
private void dfs(TreeNode node, List<Integer> nums) {
if (node != null) {
dfs(node.left, nums);
nums.add(node.val);
dfs(node.right, nums);
}
}
private TreeNode build(List<Integer> nums, int l, int r) {
if (l > r) {
return null;
}
int m = l + (r - l) / 2;
TreeNode node = new TreeNode(nums.get(m));
node.left = build(nums, l, m - 1);
node.right = build(nums, m + 1, r);
return node;
}
}