-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
48 lines (43 loc) · 711 Bytes
/
main.go
File metadata and controls
48 lines (43 loc) · 711 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
37
38
39
40
41
42
43
44
45
46
47
48
package main
import (
"log"
)
// Tree Node
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
/*
*
*
* is Same Tree
*
* args :
* p, q *TreeNode
*
*
* return value:
* bool
*
*
*/
func isSameTree(p, q *TreeNode) bool {
switch {
case p == nil && q == nil:
return true
case p == nil || q == nil || p.Val != q.Val:
return false
default:
return isSameTree(p.Left, q.Left) && isSameTree(p.Right, q.Right)
}
}
func main() {
// simple test
p := &TreeNode{1, nil, &TreeNode{1, nil, nil}}
q := &TreeNode{1, &TreeNode{1, nil, nil}, nil}
if ans := isSameTree(p, q); ans {
log.Fatalf("Worn Answer! answer equal `False` not equal : %v", ans)
}
log.Println("Answer is True!")
}