Skip to content

Commit a5bb2ac

Browse files
committed
feat: add golang solution to lc problem: No.0108.Convert Sorted Array to Binary Search Tree
1 parent d68d794 commit a5bb2ac

File tree

5 files changed

+92
-16
lines changed

5 files changed

+92
-16
lines changed

solution/0100-0199/0108.Convert Sorted Array to Binary Search Tree/README.md

+32-5
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,15 @@ class Solution {
122122
*/
123123
class Solution {
124124
public:
125-
TreeNode* sortedArrayToBST(vector<int>& nums) {
125+
TreeNode *sortedArrayToBST(vector<int> &nums) {
126126
return buildBST(nums, 0, nums.size() - 1);
127127
}
128128

129129
private:
130-
TreeNode* buildBST(vector<int>& nums, int start, int end) {
131-
if (start > end) {
130+
TreeNode *buildBST(vector<int> &nums, int start, int end) {
131+
if (start > end)
132132
return nullptr;
133-
}
134-
int mid = (start + end) / 2;
133+
int mid = start + end >> 1;
135134
TreeNode *root = new TreeNode(nums[mid]);
136135
root->left = buildBST(nums, start, mid - 1);
137136
root->right = buildBST(nums, mid + 1, end);
@@ -171,6 +170,34 @@ var sortedArrayToBST = function(nums) {
171170
};
172171
```
173172

173+
### **Go**
174+
175+
```go
176+
/**
177+
* Definition for a binary tree node.
178+
* type TreeNode struct {
179+
* Val int
180+
* Left *TreeNode
181+
* Right *TreeNode
182+
* }
183+
*/
184+
func sortedArrayToBST(nums []int) *TreeNode {
185+
return buildBST(nums, 0, len(nums)-1)
186+
}
187+
188+
func buildBST(nums []int, start, end int) *TreeNode {
189+
if start > end {
190+
return nil
191+
}
192+
mid := (start + end) >> 1
193+
return &TreeNode{
194+
Val: nums[mid],
195+
Left: buildBST(nums, start, mid-1),
196+
Right: buildBST(nums, mid+1, end),
197+
}
198+
}
199+
```
200+
174201
### **...**
175202

176203
```

solution/0100-0199/0108.Convert Sorted Array to Binary Search Tree/README_EN.md

+32-5
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,15 @@ class Solution {
112112
*/
113113
class Solution {
114114
public:
115-
TreeNode* sortedArrayToBST(vector<int>& nums) {
115+
TreeNode *sortedArrayToBST(vector<int> &nums) {
116116
return buildBST(nums, 0, nums.size() - 1);
117117
}
118118

119119
private:
120-
TreeNode* buildBST(vector<int>& nums, int start, int end) {
121-
if (start > end) {
120+
TreeNode *buildBST(vector<int> &nums, int start, int end) {
121+
if (start > end)
122122
return nullptr;
123-
}
124-
int mid = (start + end) / 2;
123+
int mid = start + end >> 1;
125124
TreeNode *root = new TreeNode(nums[mid]);
126125
root->left = buildBST(nums, start, mid - 1);
127126
root->right = buildBST(nums, mid + 1, end);
@@ -161,6 +160,34 @@ var sortedArrayToBST = function(nums) {
161160
};
162161
```
163162

163+
### **Go**
164+
165+
```go
166+
/**
167+
* Definition for a binary tree node.
168+
* type TreeNode struct {
169+
* Val int
170+
* Left *TreeNode
171+
* Right *TreeNode
172+
* }
173+
*/
174+
func sortedArrayToBST(nums []int) *TreeNode {
175+
return buildBST(nums, 0, len(nums)-1)
176+
}
177+
178+
func buildBST(nums []int, start, end int) *TreeNode {
179+
if start > end {
180+
return nil
181+
}
182+
mid := (start + end) >> 1
183+
return &TreeNode{
184+
Val: nums[mid],
185+
Left: buildBST(nums, start, mid-1),
186+
Right: buildBST(nums, mid+1, end),
187+
}
188+
}
189+
```
190+
164191
### **...**
165192

166193
```

solution/0100-0199/0108.Convert Sorted Array to Binary Search Tree/Solution.cpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,15 @@
1111
*/
1212
class Solution {
1313
public:
14-
TreeNode* sortedArrayToBST(vector<int>& nums) {
14+
TreeNode *sortedArrayToBST(vector<int> &nums) {
1515
return buildBST(nums, 0, nums.size() - 1);
1616
}
1717

1818
private:
19-
TreeNode* buildBST(vector<int>& nums, int start, int end) {
20-
if (start > end) {
19+
TreeNode *buildBST(vector<int> &nums, int start, int end) {
20+
if (start > end)
2121
return nullptr;
22-
}
23-
int mid = (start + end) / 2;
22+
int mid = start + end >> 1;
2423
TreeNode *root = new TreeNode(nums[mid]);
2524
root->left = buildBST(nums, start, mid - 1);
2625
root->right = buildBST(nums, mid + 1, end);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* type TreeNode struct {
4+
* Val int
5+
* Left *TreeNode
6+
* Right *TreeNode
7+
* }
8+
*/
9+
func sortedArrayToBST(nums []int) *TreeNode {
10+
return buildBST(nums, 0, len(nums)-1)
11+
}
12+
13+
func buildBST(nums []int, start, end int) *TreeNode {
14+
if start > end {
15+
return nil
16+
}
17+
mid := (start + end) >> 1
18+
return &TreeNode{
19+
Val: nums[mid],
20+
Left: buildBST(nums, start, mid-1),
21+
Right: buildBST(nums, mid+1, end),
22+
}
23+
}

solution/0200-0299/0289.Game of Life/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<!-- 这里写题目描述 -->
88

9-
<p>根据 <a href="https://baike.baidu.com/item/%E7%94%9F%E5%91%BD%E6%B8%B8%E6%88%8F/2926434?fr=aladdin" target="_blank">百度百科</a> ,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在 1970 年发明的细胞自动机。</p>
9+
<p>根据 <a href="https://baike.baidu.com/item/%E7%94%9F%E5%91%BD%E6%B8%B8%E6%88%8F/2926434?fr=aladdin" target="_blank">百度百科</a> ,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在 1970 年发明的 cellular automaton。</p>
1010

1111
<p>给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细胞。每个细胞都具有一个初始状态:1 即为活细胞(live),或 0 即为死细胞(dead)。每个细胞与其八个相邻位置(水平,垂直,对角线)的细胞都遵循以下四条生存定律:</p>
1212

0 commit comments

Comments
 (0)