Skip to content

Commit 5dcf6ec

Browse files
committed
[Tree] Add a solution to Convert Sorted Array to Binary Search Tree
1 parent 1554fd7 commit 5dcf6ec

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

Diff for: README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
![Leetcode](./logo.png?style=centerme)
55

66
## Progress
7-
[Problem Status](#problem-status) shows the latest progress to all 1000+ questions. Currently we have 278 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them.
7+
[Problem Status](#problem-status) shows the latest progress to all 1000+ questions. Currently we have 279 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them.
88

99
## Contributors
1010

@@ -173,6 +173,7 @@
173173
[Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree/)| [Swift](./Tree/BalancedBinaryTree.swift)| Easy| O(n)| O(n)|
174174
[Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/)| [Swift](./Tree/SumLeftLeaves.swift)| Easy| O(n)| O(1)|
175175
[Flatten Binary Tree to Linked List](https://leetcode.com/problems/flatten-binary-tree-to-linked-list/)| [Swift](./Tree/FlattenBinaryTreeLinkedList.swift)| Medium| O(n)| O(1)|
176+
[Convert Sorted Array to Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/)| [Swift](./Tree/ConvertSortedArrayBinarySearchTree.swift)| Easy| O(n)| O(1)|
176177
[Validate Binary Search Tree](https://leetcode.com/problems/validate-binary-search-tree/)| [Swift](./Tree/ValidateBinarySearchTree.swift)| Medium| O(n)| O(log n)|
177178
[Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/)| [Swift](./Tree/BinaryTreeLevelOrderTraversal.swift)| Easy| O(n)| O(n)|
178179
[Binary Tree Level Order Traversal II](https://leetcode.com/problems/binary-tree-level-order-traversal-ii/)| [Swift](./Tree/BinaryTreeLevelOrderTraversalII.swift)| Easy| O(n)| O(n)|
@@ -741,7 +742,7 @@
741742
| [Swift](./Tree/MinimumDepthOfBinaryTree.swift) | 111 | [Minimum Depth of Binary Tree](https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/) | Easy |
742743
| [Swift](./Tree/BalancedBinaryTree.swift) | 110 | [Balanced Binary Tree](https://oj.leetcode.com/problems/balanced-binary-tree/) | Easy |
743744
| | 109 | [Convert Sorted List to Binary Search Tree](https://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/) | Medium |
744-
| | 108 | [Convert Sorted Array to Binary Search Tree](https://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/) | Medium |
745+
| [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 |
745746
| [Swift](./Tree/BinaryTreeLevelOrderTraversalII.swift) | 107 | [Binary Tree Level Order Traversal II](https://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/) | Easy |
746747
| [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 |
747748
| [Swift](./Tree/ConstructBinaryTreePreorderInorder.swift) | 105 | [Construct Binary Tree from Preorder and Inorder Traversal](https://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/) | Medium |

Diff for: Tree/ConvertSortedArrayBinarySearchTree.swift

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/
3+
* Primary idea: recursion, the root of subtree should always be mid point of the subarray
4+
* Time Complexity: O(n), Space Complexity: O(1)
5+
*
6+
* Definition for a binary tree node.
7+
* public class TreeNode {
8+
* public var val: Int
9+
* public var left: TreeNode?
10+
* public var right: TreeNode?
11+
* public init(_ val: Int) {
12+
* self.val = val
13+
* self.left = nil
14+
* self.right = nil
15+
* }
16+
* }
17+
*/
18+
19+
class ConvertSortedArrayBinarySearchTree {
20+
func sortedArrayToBST(_ nums: [Int]) -> TreeNode? {
21+
return sortedArrayToBST(nums, 0, nums.count - 1)
22+
}
23+
24+
private func sortedArrayToBST(_ nums: [Int], _ leftIdx: Int, _ rightIdx: Int) -> TreeNode? {
25+
guard leftIdx <= rightIdx else {
26+
return nil
27+
}
28+
29+
let mid = (rightIdx - leftIdx) / 2 + leftIdx
30+
let root = TreeNode(nums[mid])
31+
32+
root.left = sortedArrayToBST(nums, leftIdx, mid - 1)
33+
root.right = sortedArrayToBST(nums, mid + 1, rightIdx)
34+
35+
return root
36+
}
37+
}

0 commit comments

Comments
 (0)