๐Ÿ“ฆ jtr109 / go-playground

๐Ÿ“„ lib_test.go ยท 56 lines
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
56package binarytreelevelordertraversalii

import (
	"testing"

	"github.com/jtr109/lcutils/nilint"
	"github.com/jtr109/lcutils/treenode"
	"github.com/stretchr/testify/assert"
)

func TestLevelOrder(t *testing.T) {
	root := treenode.FromSlice([]nilint.NilInt{
		nilint.NewInt(3),
		nilint.NewInt(9),
		nilint.NewInt(20),
		nilint.NewNil(),
		nilint.NewNil(),
		nilint.NewInt(15),
		nilint.NewInt(7),
	})
	expected := [][]int{{3}, {9, 20}, {15, 7}}
	actual := levelOrder(root)
	assert.Equal(t, expected, actual)
}

func TestExample1(t *testing.T) {
	root := treenode.FromSlice([]nilint.NilInt{
		nilint.NewInt(3),
		nilint.NewInt(9),
		nilint.NewInt(20),
		nilint.NewNil(),
		nilint.NewNil(),
		nilint.NewInt(15),
		nilint.NewInt(7),
	})
	expected := [][]int{{15, 7}, {9, 20}, {3}}
	actual := levelOrderBottom(root)
	assert.Equal(t, expected, actual)
}

func TestExample2(t *testing.T) {
	root := treenode.FromSlice([]nilint.NilInt{
		nilint.NewInt(1),
	})
	actual := levelOrderBottom(root)
	expected := [][]int{{1}}
	assert.Equal(t, expected, actual)
}

func TestExample3(t *testing.T) {
	root := treenode.FromSlice([]nilint.NilInt{})
	actual := levelOrderBottom(root)
	expected := [][]int{}
	assert.Equal(t, expected, actual)
}