Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Linked List] Add a solution to Convert Sorted List to Binary Search … #270

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions LinkedList/sortedListToBST.swift
Original file line number Diff line number Diff line change
@@ -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
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think you can do something like this below, so you don't have to implicitly unwrap the variable which is not recommended in production code?

        while let newCurr = cur {
            sortedArr.append(newCurr.val)
            cur = newCurr.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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the >> operation is so cool! 👍


let root = TreeNode(sorted[mid])

root.left = _helper(sortedArr, left, mid - 1)
root.right = _helper(sortedArr, mid + 1, right)

return root
}
}
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down Expand Up @@ -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 |
Expand Down