forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobzva.go
More file actions
38 lines (34 loc) · 752 Bytes
/
obzva.go
File metadata and controls
38 lines (34 loc) · 752 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
/*
풀이
- 재귀함수를 이용해서 풀이할 수 있습니다
Big O
- N: 트리 노드의 개수
- H: 트리의 높이 (logN <= H <= N)
- Time complexity: O(N)
- 모든 노드를 최대 1번 탐색합니다
- Space complexity: O(H)
- 재귀 호출 스택의 깊이는 H에 비례하여 증가합니다
*/
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func isSameTree(p *TreeNode, q *TreeNode) bool {
// base case
if p == nil && q == nil {
return true
} else if p == nil || q == nil {
return false
}
if p.Val != q.Val {
return false
}
if !isSameTree(p.Left, q.Left) || !isSameTree(p.Right, q.Right) {
return false
}
return true
}