Skip to content

Commit a81e84f

Browse files
committed
Solution: Same Tree
1 parent edca7a0 commit a81e84f

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

โ€Žsame-tree/flynn.goโ€Ž

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
ํ’€์ด
3+
- ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ ํ’€์ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค
4+
Big O
5+
- N: ํŠธ๋ฆฌ ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜
6+
- H: ํŠธ๋ฆฌ์˜ ๋†’์ด (logN <= H <= N)
7+
- Time complexity: O(N)
8+
- ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ์ตœ๋Œ€ 1๋ฒˆ ํƒ์ƒ‰ํ•ฉ๋‹ˆ๋‹ค
9+
- Space complexity: O(H)
10+
- ์žฌ๊ท€ ํ˜ธ์ถœ ์Šคํƒ์˜ ๊นŠ์ด๋Š” H์— ๋น„๋ก€ํ•˜์—ฌ ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค
11+
*/
12+
13+
/**
14+
* Definition for a binary tree node.
15+
* type TreeNode struct {
16+
* Val int
17+
* Left *TreeNode
18+
* Right *TreeNode
19+
* }
20+
*/
21+
func isSameTree(p *TreeNode, q *TreeNode) bool {
22+
// base case
23+
if p == nil && q == nil {
24+
return true
25+
} else if p == nil || q == nil {
26+
return false
27+
}
28+
29+
if p.Val != q.Val {
30+
return false
31+
}
32+
33+
if !isSameTree(p.Left, q.Left) || !isSameTree(p.Right, q.Right) {
34+
return false
35+
}
36+
37+
return true
38+
}

0 commit comments

Comments
ย (0)