Skip to content

Commit 833a178

Browse files
committed
feat(solutions): add convert_sorted_array_to_binary_search_tree
1 parent 73a8654 commit 833a178

File tree

5 files changed

+136
-0
lines changed

5 files changed

+136
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ If you like it then you can put a :star:**Star** on it.
3434
3535
| # | **Problem** & **Solution** | Difficulty | Single Repetition Duration | LeetCode Run Time |
3636
| ---: | :----- | :----------: | ----------: | ----------: |
37+
|[108][Solutions-108]|[Convert Sorted Array to Binary Search Tree][Solutions-108-Home]|Easy|[138 ns/op][Solutions-108-Code] / [5 test cases][Solutions-108-Test]|204 ms|
3738
|[107][Solutions-107]|[Binary Tree Level Order Traversal II][Solutions-107-Home]|Easy|[102 ns/op][Solutions-107-Code] / [3 test cases][Solutions-107-Test]|8 ms|
3839
|[106][Solutions-106]|[Construct Binary Tree from Inorder and Postorder Traversal][Solutions-106-Home]|Medium|[60.0 ns/op][Solutions-106-Code] / [3 test cases][Solutions-106-Test]|32 ms|
3940
|[105][Solutions-105]|[Construct Binary Tree from Preorder and Inorder Traversal][Solutions-105-Home]|Medium|[60.0 ns/op][Solutions-105-Code] / [3 test cases][Solutions-105-Test]|36 ms|
@@ -163,6 +164,10 @@ Welcome to report bugs, suggest ideas and discuss on [issues page](https://githu
163164
## License
164165
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2FWindomZ%2Fleetcode.go.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2FWindomZ%2Fleetcode.go?ref=badge_large)
165166

167+
[Solutions-108]:https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/
168+
[Solutions-108-Home]:solutions/convert_sorted_array_to_binary_search_tree/
169+
[Solutions-108-Code]:solutions/convert_sorted_array_to_binary_search_tree/convertsortedarraytobinarysearchtree.go
170+
[Solutions-108-Test]:solutions/convert_sorted_array_to_binary_search_tree/convertsortedarraytobinarysearchtree_test.go#L59
166171
[Solutions-107]:https://leetcode.com/problems/binary-tree-level-order-traversal-ii/
167172
[Solutions-107-Home]:solutions/binary_tree_level_order_traversal_ii/
168173
[Solutions-107-Code]:solutions/binary_tree_level_order_traversal_ii/binarytreelevelordertraversalii.go
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# 解法要点
2+
1. 二分法解法思想,通过递归处理叶分支
3+
1. 注意中间值越界的处理
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# [108. Convert Sorted Array to Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/description/)
2+
3+
## Description
4+
5+
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
6+
7+
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of _every_ node never differ by more than 1.
8+
9+
**Example:**
10+
11+
Given the sorted array: [-10,-3,0,5,9],
12+
13+
One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST:
14+
15+
0
16+
/ \
17+
-3 9
18+
/ /
19+
-10 5
20+
21+
## Solution
22+
- [Code](convertsortedarraytobinarysearchtree.go)
23+
- [Testing](convertsortedarraytobinarysearchtree_test.go)
24+
25+
## Note
26+
- [中文](NOTE_Ch-zh.md)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package convertsortedarraytobinarysearchtree
2+
3+
// TreeNode definition for a binary tree node.
4+
type TreeNode struct {
5+
Val int
6+
Left *TreeNode
7+
Right *TreeNode
8+
}
9+
10+
func sortedArrayToBST(nums []int) *TreeNode {
11+
if len(nums) == 0 {
12+
return nil
13+
}
14+
return sortedArray(&nums, 0, len(nums)-1)
15+
}
16+
17+
func sortedArray(nums *[]int, low, high int) *TreeNode {
18+
if low > high { // Done
19+
return nil
20+
}
21+
mid := low + (high-low)/2
22+
if (high-low)%2 != 0 {
23+
mid++
24+
}
25+
return &TreeNode{
26+
Val: (*nums)[mid],
27+
Left: sortedArray(nums, low, mid-1),
28+
Right: sortedArray(nums, mid+1, high),
29+
}
30+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package convertsortedarraytobinarysearchtree
2+
3+
import (
4+
"testing"
5+
6+
"github.com/WindomZ/testify/assert"
7+
)
8+
9+
func Test_sortedArrayToBST(t *testing.T) {
10+
assert.Empty(t, sortedArrayToBST(nil))
11+
assert.Empty(t, sortedArrayToBST([]int{}))
12+
assert.Equal(t, &TreeNode{
13+
Val: 1,
14+
}, sortedArrayToBST([]int{1}))
15+
assert.Equal(t, &TreeNode{
16+
Val: 2,
17+
Left: &TreeNode{
18+
Val: 1,
19+
},
20+
}, sortedArrayToBST([]int{1, 2}))
21+
assert.Equal(t, &TreeNode{
22+
Val: 0,
23+
Left: &TreeNode{
24+
Val: -3,
25+
Left: &TreeNode{
26+
Val: -10,
27+
},
28+
},
29+
Right: &TreeNode{
30+
Val: 9,
31+
Left: &TreeNode{
32+
Val: 5,
33+
},
34+
},
35+
}, sortedArrayToBST([]int{-10, -3, 0, 5, 9}))
36+
assert.Equal(t, &TreeNode{
37+
Val: 0,
38+
Left: &TreeNode{
39+
Val: -10,
40+
Left: &TreeNode{
41+
Val: -11,
42+
},
43+
Right: &TreeNode{
44+
Val: -3,
45+
},
46+
},
47+
Right: &TreeNode{
48+
Val: 9,
49+
Left: &TreeNode{
50+
Val: 5,
51+
},
52+
Right: &TreeNode{
53+
Val: 12,
54+
},
55+
},
56+
}, sortedArrayToBST([]int{-11, -10, -3, 0, 5, 9, 12}))
57+
}
58+
59+
func Benchmark_sortedArrayToBST(b *testing.B) {
60+
b.StopTimer()
61+
b.ReportAllocs()
62+
b.StartTimer()
63+
b.RunParallel(func(pb *testing.PB) {
64+
for pb.Next() {
65+
sortedArrayToBST([]int{})
66+
sortedArrayToBST([]int{1})
67+
sortedArrayToBST([]int{1, 2})
68+
sortedArrayToBST([]int{-10, -3, 0, 5, 9})
69+
sortedArrayToBST([]int{-11, -10, -3, 0, 5, 9, 12})
70+
}
71+
})
72+
}

0 commit comments

Comments
 (0)