diff --git a/LinkedList/sortedListToBST.swift b/LinkedList/sortedListToBST.swift new file mode 100644 index 00000000..98ec414a --- /dev/null +++ b/LinkedList/sortedListToBST.swift @@ -0,0 +1,57 @@ +/** + * Question Link: https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ + * Primary idea: Convert the linked list to array, handle array using Divide and Conqure + * Time Complexity Per Action: O(N), Space Complexity: O(N) + * + */ + + /** + * Definition for singly-linked list. + * public class ListNode { + * public var val: Int + * public var next: ListNode? + * public init(_ val: Int) { + * self.val = val + * self.next = nil + * } + * } + */ +/** + * 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 Solution { + func sortedListToBST(_ head: ListNode?) -> TreeNode? { + var sortedArr: [Int] = [] + + var cur = head + while cur != nil { + sortedArr.append(cur!.val) + cur = cur!.next + } + + return _helper(sortedArr, 0, sortedArr.count - 1) + } + + private func _helper(_ sortedArr: [Int], _ left: Int, _ right: Int) -> TreeNode? { + guard left <= right else { return nil } + + let mid = left + (right - left) >> 1 + + let root = TreeNode(sorted[mid]) + + root.left = _helper(sortedArr, left, mid - 1) + root.right = _helper(sortedArr, mid + 1, right) + + return root + } +} \ No newline at end of file diff --git a/README.md b/README.md index e96ef029..6e3c0ac5 100644 --- a/README.md +++ b/README.md @@ -149,6 +149,7 @@ [Partition List](https://leetcode.com/problems/partition-list/)| [Swift](./LinkedList/PartitionList.swift)| Medium| O(n)| O(1)| [LRU Cache](https://leetcode.com/problems/lru-cache/) | [Swift](./LinkedList/LRUCache.swift) | Hard| O(1)| O(1)| [LFU Cache](https://leetcode.com/problems/lfu-cache/) | [Swift](./LinkedList/LFUCache.swift) | Hard| O(1)| O(1)| +[Convert Sorted List to Binary Search Tree](https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/) | [Swift](./LinkedList/sortedListToBST) | Medium| O(n)| O(n)| ## Stack | Title | Solution | Difficulty | Time | Space | @@ -762,7 +763,7 @@ | [Swift](./Tree/PathSum.swift) | 112 | [Path Sum](https://oj.leetcode.com/problems/path-sum/) | Easy | | [Swift](./Tree/MinimumDepthOfBinaryTree.swift) | 111 | [Minimum Depth of Binary Tree](https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/) | Easy | | [Swift](./Tree/BalancedBinaryTree.swift) | 110 | [Balanced Binary Tree](https://oj.leetcode.com/problems/balanced-binary-tree/) | Easy | -| | 109 | [Convert Sorted List to Binary Search Tree](https://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/) | Medium | +| [Swift](./LinkedList/sortedListToBST) | 109 | [Convert Sorted List to Binary Search Tree](https://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/) | Medium | | [Swift](./Tree/ConvertSortedArrayBinarySearchTree.swift) | 108 | [Convert Sorted Array to Binary Search Tree](https://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/) | Medium | | [Swift](./Tree/BinaryTreeLevelOrderTraversalII.swift) | 107 | [Binary Tree Level Order Traversal II](https://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/) | Easy | | [Swift](./Tree/ConstructBinaryTreeInorderPostorder.swift) | 106 | [Construct Binary Tree from Inorder and Postorder Traversal](https://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/) | Medium |