-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelper_test.go
95 lines (82 loc) · 1.79 KB
/
helper_test.go
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
package leetcode
import (
"github.com/stretchr/testify/require"
"testing"
)
func TestPrintTree(t *testing.T) {
input := `[9,6,-3,null,null,-6,2,null,null,2,null,-6,-6,-6]`
tree := ParseTreeFromInput(input)
PrintTree(``, tree)
}
func TestRenderTree(t *testing.T) {
input := `[9,6,-3,null,null,-6,2,null,null,2,null,-6,-6,-6]`
tree := ParseTreeFromInput(input)
RenderTree(tree)
}
func TestGraph(t *testing.T) {
input := `[[4,3,1],[3,2,4],[3],[4],[]]`
graph := ParseGraphInput(input)
RenderGraph(graph)
}
func TestGraphEdges(t *testing.T) {
input := `[[0,1],[0,2],[2,5],[3,4],[4,2]]`
edges := ParseEdgesInput(input)
RenderGraphByEdges(edges)
}
func TestLinkedList(t *testing.T) {
input := `1,2,3,4,5,6`
head := ParseLinkedListFromStr(input)
// get int linked list instead of string
//head := GetIntLinkedListFromStr(input)
PrintLinkedListNode(head)
}
func TestIntMatrix(t *testing.T) {
input := `[[0,0,1,1],[1,0,1,0],[1,1,0,0]]`
matrix := ParseIntMatrix(input)
PrintIntMatrix(matrix)
}
func TestIntArray(t *testing.T) {
input := `[0,0,1,1]`
ParseIntSlice(input)
}
func goodNodes(root *TreeNode) int {
return 1 + goodNodesHelper(root, root.Val)
}
func goodNodesHelper(p *TreeNode, max int) int {
if p == nil {
return 0
}
if p.Left != nil {
lm := max
cnt := 0
if p.Left.Val >= lm {
lm = p.Left.Val
cnt = 1
}
return cnt + goodNodesHelper(p.Left, lm)
}
if p.Right != nil {
rm := max
cnt := 0
if p.Right.Val >= rm {
rm = p.Right.Val
cnt = 1
}
return cnt + goodNodesHelper(p.Right, rm)
}
return 0
}
func TestTmp(t *testing.T) {
inputs := []string{
`[3,1,4,3,null,1,5]`,
`[3,3,null,4,2]`,
`[1]`,
}
res := []int{4, 3, 1}
for k, v := range inputs {
in := ParseTreeFromInput(v)
require.Equal(t, res[k], goodNodes(in))
}
}
func TestTmp2(t *testing.T) {
}