File tree Expand file tree Collapse file tree 3 files changed +115
-0
lines changed
solution/0100-0199/0109.Convert Sorted List to Binary Search Tree Expand file tree Collapse file tree 3 files changed +115
-0
lines changed Original file line number Diff line number Diff line change @@ -380,6 +380,46 @@ struct TreeNode *sortedListToBST(struct ListNode *head) {
380
380
}
381
381
```
382
382
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
+
383
423
### ** ...**
384
424
385
425
```
Original file line number Diff line number Diff line change @@ -366,6 +366,46 @@ struct TreeNode *sortedListToBST(struct ListNode *head) {
366
366
}
367
367
```
368
368
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
+
369
409
### ** ...**
370
410
371
411
```
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments