Skip to content

Commit 6014b39

Browse files
authored
feat: add golang solution to lc problem: No.0109 (#925)
No.0109. Convert Sorted List to Binary Search Tree
1 parent e664b42 commit 6014b39

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,46 @@ struct TreeNode *sortedListToBST(struct ListNode *head) {
380380
}
381381
```
382382
383+
### **Go**
384+
385+
```go
386+
/**
387+
* Definition for singly-linked list.
388+
* type ListNode struct {
389+
* Val int
390+
* Next *ListNode
391+
* }
392+
*/
393+
/**
394+
* Definition for a binary tree node.
395+
* type TreeNode struct {
396+
* Val int
397+
* Left *TreeNode
398+
* Right *TreeNode
399+
* }
400+
*/
401+
func sortedListToBST(head *ListNode) *TreeNode {
402+
nums := []int{}
403+
for head != nil {
404+
nums = append(nums, head.Val)
405+
head = head.Next
406+
}
407+
return buildBST(nums, 0, len(nums)-1)
408+
}
409+
410+
func buildBST(nums []int, start, end int) *TreeNode {
411+
if start > end {
412+
return nil
413+
}
414+
mid := (start + end) >> 1
415+
return &TreeNode{
416+
Val: nums[mid],
417+
Left: buildBST(nums, start, mid-1),
418+
Right: buildBST(nums, mid+1, end),
419+
}
420+
}
421+
```
422+
383423
### **...**
384424

385425
```

solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/README_EN.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,46 @@ struct TreeNode *sortedListToBST(struct ListNode *head) {
366366
}
367367
```
368368
369+
### **Go**
370+
371+
```go
372+
/**
373+
* Definition for singly-linked list.
374+
* type ListNode struct {
375+
* Val int
376+
* Next *ListNode
377+
* }
378+
*/
379+
/**
380+
* Definition for a binary tree node.
381+
* type TreeNode struct {
382+
* Val int
383+
* Left *TreeNode
384+
* Right *TreeNode
385+
* }
386+
*/
387+
func sortedListToBST(head *ListNode) *TreeNode {
388+
nums := []int{}
389+
for head != nil {
390+
nums = append(nums, head.Val)
391+
head = head.Next
392+
}
393+
return buildBST(nums, 0, len(nums)-1)
394+
}
395+
396+
func buildBST(nums []int, start, end int) *TreeNode {
397+
if start > end {
398+
return nil
399+
}
400+
mid := (start + end) >> 1
401+
return &TreeNode{
402+
Val: nums[mid],
403+
Left: buildBST(nums, start, mid-1),
404+
Right: buildBST(nums, mid+1, end),
405+
}
406+
}
407+
```
408+
369409
### **...**
370410

371411
```
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* type ListNode struct {
4+
* Val int
5+
* Next *ListNode
6+
* }
7+
*/
8+
/**
9+
* Definition for a binary tree node.
10+
* type TreeNode struct {
11+
* Val int
12+
* Left *TreeNode
13+
* Right *TreeNode
14+
* }
15+
*/
16+
func sortedListToBST(head *ListNode) *TreeNode {
17+
nums := []int{}
18+
for head != nil {
19+
nums = append(nums, head.Val)
20+
head = head.Next
21+
}
22+
return buildBST(nums, 0, len(nums)-1)
23+
}
24+
25+
func buildBST(nums []int, start, end int) *TreeNode {
26+
if start > end {
27+
return nil
28+
}
29+
mid := (start + end) >> 1
30+
return &TreeNode{
31+
Val: nums[mid],
32+
Left: buildBST(nums, start, mid-1),
33+
Right: buildBST(nums, mid+1, end),
34+
}
35+
}

0 commit comments

Comments
 (0)