-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0109.Convert Sorted List to Binary Search Tree.swift
51 lines (46 loc) · 1.45 KB
/
0109.Convert Sorted List to Binary Search Tree.swift
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
/**
* Definition for singly-linked list.
* public class ListNode {
* public var val: Int
* public var next: ListNode?
* public init() { self.val = 0; self.next = nil; }
* public init(_ val: Int) { self.val = val; self.next = nil; }
* public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
* }
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* public var val: Int
* public var left: TreeNode?
* public var right: TreeNode?
* public init() { self.val = 0; self.left = nil; self.right = nil; }
* public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
* public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
* self.val = val
* self.left = left
* self.right = right
* }
* }
*/
class Solution {
private(set) var _nums = [Int]()
func sortedListToBST(_ head: ListNode?) -> TreeNode? {
var pointer = head
while pointer != nil {
_nums.append(pointer!.val)
pointer = pointer!.next
}
return transform(0, _nums.count - 1)
}
private func transform(_ s: Int, _ e: Int) -> TreeNode? {
if e < s {
return nil
}
if s == e {
return TreeNode(_nums[s])
}
let mid = s + (e - s + 1) / 2
return TreeNode(_nums[mid], transform(s, mid - 1), transform(mid + 1, e))
}
}