-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLC109-Convert-Sorted-List-to-Binary-Search-Tree.java
67 lines (58 loc) · 1.76 KB
/
LC109-Convert-Sorted-List-to-Binary-Search-Tree.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
/*
Given the head of a singly linked list where elements are sorted in ascending
order, convert it to a height balanced BST.
For this problem, a height-balanced binary tree is defined as a binary tree in
which the depth of the two subtrees of every node never differ by more than 1.
Example 1:
Input: head = [-10,-3,0,5,9]
Output: [0,-3,9,-10,null,5]
Explanation: One possible answer is [0,-3,9,-10,null,5], which represents
the shown height balanced BST.
Example 2:
Input: head = []
Output: []
Example 3:
Input: head = [0]
Output: [0]
Example 4:
Input: head = [1,3]
Output: [3,1]
Constraints:
The number of nodes in head is in the range [0, 2 * 10^4].
-10^5 <= Node.val <= 10^5
*/
class Solution {
public TreeNode sortedListToBST(ListNode head) {
if (head == null){
return null;
}
ListNode head2 = head;
int length = 0;
while (head2 != null){
length++;
head2 = head2.next;
}
return sortedListToBSTHelper(head, length);
}
private TreeNode sortedListToBSTHelper(ListNode head, int length){
if (length == 0){
return null;
} else if (length == 1){
return new TreeNode(head.val);
}
// find mid-node and make it a head
int mid_length = length / 2;
ListNode temp = head;
for (int i = 0; i < mid_length - 1; i++){
temp = temp.next;
}
// split LinkedList into two
ListNode rightTree = temp.next;
temp.next = null;
TreeNode root = new TreeNode(rightTree.val);
rightTree = rightTree.next;
root.left = sortedListToBSTHelper(head, mid_length);
root.right = sortedListToBSTHelper(rightTree, length - 1 - mid_length);
return root;
}
}