-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunLists.java
More file actions
37 lines (33 loc) · 981 Bytes
/
funLists.java
File metadata and controls
37 lines (33 loc) · 981 Bytes
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
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
class Solution {
static TreeNode flatten(ListNode head) {
if(head == null)
return null;
List<Integer> arr = new ArrayList<>();
getNums(head, arr);
Collections.sort(arr);
return makeTree(arr, 0);
}
public static TreeNode makeTree(List<Integer> arr, int i){
if(i >= arr.size())
return null;
return new TreeNode(arr.get(i), makeTree(arr, i * 2 + 1), makeTree(arr, i * 2 + 2));
}
public static void getNums(ListNode list, List<Integer> arr){
if(list != null){
getNums(list.data, arr);
getNums(list.next, arr);
}
}
public static void getNums(TreeNode tree, List<Integer> arr){
if(tree != null){
if(!arr.contains(tree.value))
arr.add(tree.value);
getNums(tree.left, arr);
getNums(tree.right, arr);
}
}
}
//https://www.codewars.com/kata/5866ce53dbca9af9940000d3/train/javascript