forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1382.java
35 lines (30 loc) · 1.02 KB
/
_1382.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
package com.fishercoder.solutions;
import com.fishercoder.common.classes.TreeNode;
import java.util.ArrayList;
import java.util.List;
public class _1382 {
public static class Solution1 {
public TreeNode balanceBST(TreeNode root) {
List<Integer> inorder = inorder(root, new ArrayList<>());
return dfs(inorder, 0, inorder.size() - 1);
}
private List<Integer> inorder(TreeNode root, List<Integer> list) {
if (root == null) {
return list;
}
inorder(root.left, list);
list.add(root.val);
return inorder(root.right, list);
}
private TreeNode dfs(List<Integer> nums, int start, int end) {
if (end < start) {
return null;
}
int mid = (start + end) / 2;
TreeNode root = new TreeNode(nums.get(mid));
root.left = dfs(nums, start, mid - 1);
root.right = dfs(nums, mid + 1, end);
return root;
}
}
}