forked from spring1843/go-dsa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
is_tree_symmetrical_test.go
48 lines (42 loc) · 1.07 KB
/
is_tree_symmetrical_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
package queue
import (
"testing"
"github.com/spring1843/go-dsa/tree"
)
/*
TestIsTreeSymmetrical tests solution(s) with the following signature and problem description:
func IsTreeSymmetrical(root *tree.BinaryTreeNode) (bool, error)
Given a binary tree the one above "2,4,4,5,6,6,5", true if it is symmetric (mirrored from the root), and false otherwise.
2
/ \
/ \
4 4
/ \ / \
5 6 6 5
*/
func TestIsTreeSymmetrical(t *testing.T) {
tests := []struct {
tree string
isSymmetric bool
}{
{"", false},
{"1", true},
{"1,2,2", true},
{"1,2,3", false},
{"1,2,2,3,nil,nil,3", true},
{"1,2,nil,4", false},
{"1,2,3,4,nil,5,6", false},
{"1,2,nil,4,nil,5,6", false},
{"2,4,4,5,6,5,6", false},
{"2,4,4,5,6,6,5", true},
}
for i, test := range tests {
got, err := IsTreeSymmetrical(tree.Unserialize(test.tree))
if err != nil {
t.Fatalf("Failed test case #%d. Unexpected error %s", i, err)
}
if got != test.isSymmetric {
t.Fatalf("Failed test case #%d. Want %t got %t", i, test.isSymmetric, got)
}
}
}