-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsymmetric-tree.go
More file actions
36 lines (31 loc) · 813 Bytes
/
Copy pathsymmetric-tree.go
File metadata and controls
36 lines (31 loc) · 813 Bytes
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
package main
// TreeNode is the node type for a binary tree
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func iCantChangeTheFuncSignature(p *TreeNode, q *TreeNode) bool {
if p != nil && q != nil {
return p.Val == q.Val &&
iCantChangeTheFuncSignature(p.Left, q.Right) &&
iCantChangeTheFuncSignature(p.Right, q.Left)
}
if p == nil && q == nil {
return true
}
return false
}
func isSymmetric(root *TreeNode) bool {
return iCantChangeTheFuncSignature(root, root)
}
func main() {
test := &TreeNode{1, &TreeNode{2, nil, nil}, &TreeNode{3, nil, nil}}
println("Testing")
result := isSymmetric(test)
println("Yields", result)
test = &TreeNode{1, &TreeNode{2, nil, nil}, &TreeNode{2, nil, nil}}
println("Testing")
result = isSymmetric(test)
println("Yields", result)
}