-
Notifications
You must be signed in to change notification settings - Fork 8
/
Main.java
73 lines (65 loc) · 2.04 KB
/
Main.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package com.company;
import java.util.ArrayList;
import java.util.List;
class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
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;
}
}
public class Main {
public static void main(String[] args) {
ListNode head = generate(new int[]{-10, -3, 0, 5, 9});
sortedListToBST(head);
}
public static TreeNode sortedListToBST_list(ListNode head) {
List<Integer> nums = new ArrayList<>();
while (head != null){
nums.add(head.val);
head = head.next;
}
return dfs(nums, 0, nums.size() - 1);
}
public static TreeNode dfs(List<Integer> nums, int li, int hi){
if (hi < li) return null;
int mid = (hi + li) / 2;
TreeNode root = new TreeNode(nums.get(mid));
root.left = dfs(nums, li, mid - 1);
root.right = dfs(nums, mid + 1, hi);
return root;
}
public static TreeNode sortedListToBST(ListNode head) {
if (head == null) return null;
if (head.next == null) return new TreeNode(head.val);
ListNode pre = new ListNode(0, head), fast = head, slow = head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
pre = pre.next;
}
TreeNode root = new TreeNode(slow.val);
pre.next = null;
root.right = sortedListToBST(slow.next);
root.left = sortedListToBST(head);
return root;
}
public static ListNode generate(int[] nums){
ListNode res = null;
for (int i = nums.length - 1; i >= 0; i--)
res = new ListNode(nums[i], res);
return res;
}
}