mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 08:27:30 +08:00
添加 problem 109
This commit is contained in:
@ -0,0 +1,57 @@
|
||||
package leetcode
|
||||
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* type ListNode struct {
|
||||
* Val int
|
||||
* Next *ListNode
|
||||
* }
|
||||
*/
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* type TreeNode struct {
|
||||
* Val int
|
||||
* Left *TreeNode
|
||||
* Right *TreeNode
|
||||
* }
|
||||
*/
|
||||
|
||||
type TreeNode struct {
|
||||
Val int
|
||||
Left *TreeNode
|
||||
Right *TreeNode
|
||||
}
|
||||
|
||||
func sortedListToBST(head *ListNode) *TreeNode {
|
||||
if head == nil {
|
||||
return nil
|
||||
}
|
||||
if head != nil && head.Next == nil {
|
||||
return &TreeNode{Val: head.Val, Left: nil, Right: nil}
|
||||
}
|
||||
middleNode, preNode := middleNodeAndPreNode(head)
|
||||
if middleNode == nil {
|
||||
return nil
|
||||
}
|
||||
if preNode != nil {
|
||||
preNode.Next = nil
|
||||
}
|
||||
if middleNode == head {
|
||||
head = nil
|
||||
}
|
||||
return &TreeNode{Val: middleNode.Val, Left: sortedListToBST(head), Right: sortedListToBST(middleNode.Next)}
|
||||
}
|
||||
|
||||
func middleNodeAndPreNode(head *ListNode) (middle *ListNode, pre *ListNode) {
|
||||
if head == nil || head.Next == nil {
|
||||
return nil, head
|
||||
}
|
||||
p1 := head
|
||||
p2 := head
|
||||
for p2.Next != nil && p2.Next.Next != nil {
|
||||
pre = p1
|
||||
p1 = p1.Next
|
||||
p2 = p2.Next.Next
|
||||
}
|
||||
return p1, pre
|
||||
}
|
@ -0,0 +1,83 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question109 struct {
|
||||
para109
|
||||
ans109
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para109 struct {
|
||||
one []int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans109 struct {
|
||||
one []int
|
||||
}
|
||||
|
||||
func Test_Problem109(t *testing.T) {
|
||||
|
||||
qs := []question109{
|
||||
|
||||
question109{
|
||||
para109{[]int{-10, -3, 0, 5, 9}},
|
||||
ans109{[]int{0, -3, 9, -10, -9999, 5}},
|
||||
},
|
||||
|
||||
question109{
|
||||
para109{[]int{-10}},
|
||||
ans109{[]int{-10}},
|
||||
},
|
||||
|
||||
question109{
|
||||
para109{[]int{1, 2}},
|
||||
ans109{[]int{2, 1}},
|
||||
},
|
||||
|
||||
question109{
|
||||
para109{[]int{1, 2, 3}},
|
||||
ans109{[]int{2, 1, 3}},
|
||||
},
|
||||
// question109{
|
||||
// para109{[]int{1, 2, 3, 4, 5}, 2, 2},
|
||||
// ans109{[]int{1, 2, 3, 4, 5}},
|
||||
// },
|
||||
|
||||
// question109{
|
||||
// para109{[]int{1, 2, 3, 4, 5}, 1, 5},
|
||||
// ans109{[]int{5, 4, 3, 2, 1}},
|
||||
// },
|
||||
|
||||
// question109{
|
||||
// para109{[]int{1, 2, 3, 4, 5, 6}, 3, 4},
|
||||
// ans109{[]int{1, 2, 4, 3, 5, 6}},
|
||||
// },
|
||||
|
||||
// question109{
|
||||
// para109{[]int{3, 5}, 1, 2},
|
||||
// ans109{[]int{5, 3}},
|
||||
// },
|
||||
|
||||
// question109{
|
||||
// para109{[]int{3}, 3, 5},
|
||||
// ans109{[]int{3}},
|
||||
// },
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 109------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans109, q.para109
|
||||
arr := []int{}
|
||||
T2s(sortedListToBST(S2l(p.one)), &arr)
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, arr)
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
# [109. Convert Sorted List to Binary Search Tree](https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/)
|
||||
|
||||
## 题目
|
||||
|
||||
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
|
||||
|
||||
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.
|
||||
|
||||
Example:
|
||||
|
||||
```
|
||||
Given the sorted linked list: [-10,-3,0,5,9],
|
||||
|
||||
One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST:
|
||||
|
||||
0
|
||||
/ \
|
||||
-3 9
|
||||
/ /
|
||||
-10 5
|
||||
```
|
||||
|
||||
|
||||
## 题目大意
|
||||
|
||||
将链表转化为高度平衡的二叉搜索树。高度平衡的定义:每个结点的 2 个子结点的深度不能相差超过 1 。
|
||||
|
||||
思路比较简单,依次把链表的中间点作为根结点,类似二分的思想,递归排列所有结点即可。
|
Reference in New Issue
Block a user