-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1382-balance-a-binary-search-tree.java
More file actions
40 lines (36 loc) · 1.11 KB
/
Copy path1382-balance-a-binary-search-tree.java
File metadata and controls
40 lines (36 loc) · 1.11 KB
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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode balanceBST(TreeNode root) {
List<Integer> lst = inorder(root, new ArrayList<>());
return build(lst, 0, lst.size()-1);
}
public List<Integer> inorder(TreeNode root, List<Integer> lst) {
if (root == null) return lst;
inorder(root.left, lst);
lst.add(root.val);
inorder(root.right, lst);
return lst;
}
public TreeNode build(List<Integer> lst, int left, int right) {
if (left > right || left < 0 || right >= lst.size()) return null;
int mid = (left + right)/2;
TreeNode node = new TreeNode(lst.get(mid));
node.left = build(lst, left, mid-1);
node.right = build(lst, mid+1, right);
return node;
}
}