-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0100-same-tree.go
More file actions
36 lines (31 loc) · 777 Bytes
/
0100-same-tree.go
File metadata and controls
36 lines (31 loc) · 777 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 trees
import "github.com/shahzodshafizod/leetcode/pkg"
// https://leetcode.com/problems/same-tree/
// DFS
func isSameTree(p *pkg.TreeNode, q *pkg.TreeNode) bool {
if p == nil || q == nil {
return p == q
}
return p.Val == q.Val &&
isSameTree(p.Left, q.Left) &&
isSameTree(p.Right, q.Right)
}
// // BFS
// func isSameTree(p *pkg.TreeNode, q *pkg.TreeNode) bool {
// var queue = []*pkg.TreeNode{p, q}
// for length := len(queue); length > 0; length = len(queue) {
// p, q = queue[0], queue[1]
// queue = queue[2:]
// if p == nil || q == nil {
// if p != q {
// return false
// }
// continue
// }
// if p.Val != q.Val {
// return false
// }
// queue = append(queue, p.Left, q.Left, p.Right, q.Right)
// }
// return true
// }