forked from soapyigu/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConvertSortedArrayBinarySearchTree.swift
37 lines (32 loc) · 1.11 KB
/
ConvertSortedArrayBinarySearchTree.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
/**
* Question Link: https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/
* Primary idea: recursion, the root of subtree should always be mid point of the subarray
* Time Complexity: O(n), Space Complexity: O(1)
*
* Definition for a binary tree node.
* public class TreeNode {
* public var val: Int
* public var left: TreeNode?
* public var right: TreeNode?
* public init(_ val: Int) {
* self.val = val
* self.left = nil
* self.right = nil
* }
* }
*/
class ConvertSortedArrayBinarySearchTree {
func sortedArrayToBST(_ nums: [Int]) -> TreeNode? {
return sortedArrayToBST(nums, 0, nums.count - 1)
}
private func sortedArrayToBST(_ nums: [Int], _ leftIdx: Int, _ rightIdx: Int) -> TreeNode? {
guard leftIdx <= rightIdx else {
return nil
}
let mid = (rightIdx - leftIdx) / 2 + leftIdx
let root = TreeNode(nums[mid])
root.left = sortedArrayToBST(nums, leftIdx, mid - 1)
root.right = sortedArrayToBST(nums, mid + 1, rightIdx)
return root
}
}