Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
137 lines (122 loc) · 2.61 KB
/
Copy pathmain.go
File metadata and controls
137 lines (122 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"fmt"
"strings"
)
/**
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
*/
//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
}
func sortedListNToBST(head *ListNode, length int) *TreeNode {
var root *TreeNode
if length > 0 {
middle := head
size := length / 2
for i := size; i > 0; i-- {
middle = middle.Next
}
root = new(TreeNode)
root.Val = middle.Val
root.Left = sortedListNToBST(head, size)
root.Right = sortedListNToBST(middle.Next, length-size-1)
}
return root
}
// 2252ms, 2% shit
// difference is if you can modify original list
// if you can, length is not necessary
// if you can't, you need a length
func sortedListToBST(head *ListNode) *TreeNode {
var length int
probe := head
for probe != nil {
length++
probe = probe.Next
}
return sortedListNToBST(head, length)
}
// modify original list
func sortedListToBST2(head *ListNode) *TreeNode {
if head == nil {
return nil
}
if head.Next == nil {
return &TreeNode{Val: head.Val}
}
// find middle
probe := head
length := 0
for probe.Next != nil {
probe = probe.Next
length++
}
half := length / 2
probe = head
for half > 1 {
half--
probe = probe.Next
}
middle := probe.Next
probe.Next = nil
return &TreeNode{
Val: middle.Val,
Left: sortedListToBST2(head),
Right: sortedListToBST2(middle.Next),
}
}
// 1392ms, 52%, ok
// another method, speedup only traverse list once, nice trick
func sortedListToBST3(head *ListNode) *TreeNode {
// three common cases
if head == nil {
return nil
}
if head.Next == nil {
return &TreeNode{Val: head.Val}
}
if head.Next != nil && head.Next.Next == nil {
return &TreeNode{Val: head.Val,
Right: &TreeNode{Val: head.Next.Val}}
}
pre, normal, double := head, head, head
for double.Next != nil && double.Next.Next != nil {
pre, normal = normal, normal.Next
double = double.Next.Next
}
pre.Next = nil
return &TreeNode{
Val: normal.Val,
Left: sortedListToBST3(head),
Right: sortedListToBST3(normal.Next),
}
}
func main() {
list := &ListNode{1, &ListNode{2, &ListNode{3, nil}}}
fmt.Println(sortedListToBST3(list))
}
func (t *TreeNode) String() string {
var nodes []string
var walk func(*TreeNode)
walk = func(n *TreeNode) {
if n == nil {
return
}
walk(n.Left)
nodes = append(nodes, fmt.Sprintf("%d ", n.Val))
walk(n.Right)
}
nodes = append(nodes, "[")
walk(t)
nodes = append(nodes, "]")
return strings.Join(nodes, " ")
}