-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLeetCode-109-Convert-Sorted-List-to-Binary-Search-Tree.java
More file actions
60 lines (52 loc) · 1.52 KB
/
LeetCode-109-Convert-Sorted-List-to-Binary-Search-Tree.java
File metadata and controls
60 lines (52 loc) · 1.52 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
LeetCode: https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/
LintCode: http://www.lintcode.com/problem/convert-sorted-list-to-binary-search-tree/
JiuZhang: http://www.jiuzhang.com/solutions/convert-sorted-list-to-binary-search-tree/
ProgramCreek: http://www.programcreek.com/2013/01/leetcode-convert-sorted-list-to-binary-search-tree-java/
Analysis:
*/
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode sortedListToBST(ListNode head) {
if(head == null || getLength(head) < 1) return null;
int len = getLength(head);
return DFS(head, 0, len - 1);
}
private TreeNode DFS(ListNode head, int start, int end){
if(start > end) return null;
int i = start;
ListNode h = head;
while(i < start + (end - start) / 2){
h = h.next;
i++;
}
TreeNode mid = new TreeNode(h.val);
mid.left = DFS(head, start, i - 1);
mid.right = DFS(h.next, i + 1, end);
return mid;
}
private int getLength(ListNode head){
int len = 0;
while(head != null){
len++;
head = head.next;
}
return len;
}
}