@@ -122,16 +122,15 @@ class Solution {
122
122
*/
123
123
class Solution {
124
124
public:
125
- TreeNode* sortedArrayToBST(vector<int >& nums) {
125
+ TreeNode * sortedArrayToBST(vector<int > & nums) {
126
126
return buildBST(nums, 0, nums.size() - 1);
127
127
}
128
128
129
129
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)
132
132
return nullptr;
133
- }
134
- int mid = (start + end) / 2;
133
+ int mid = start + end >> 1;
135
134
TreeNode * root = new TreeNode(nums[ mid] );
136
135
root->left = buildBST(nums, start, mid - 1);
137
136
root->right = buildBST(nums, mid + 1, end);
@@ -171,6 +170,34 @@ var sortedArrayToBST = function(nums) {
171
170
};
172
171
```
173
172
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
+
174
201
### ** ...**
175
202
176
203
```
0 commit comments