Skip to content

Commit ea4170f

Browse files
committed
104. Maximum Depth of Binary Tree
1 parent f245112 commit ea4170f

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
8+
# ์‹œ๊ฐ„ ๋ณต์žก๋„: O(N), ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ํ•œ๋ฒˆ ์”ฉ ๋ฐฉ๋ฌธ
9+
# ๊ณต๊ฐ„ ๋ณต์žก๋„: O(N), ์ตœ์•…์˜ ๊ฒฝ์šฐ ๋ชจ๋“  ๋…ธ๋“œ๊ฐ€ ํ์— ๋‹ด๊ธธ ์ˆ˜ ์žˆ์Œ
10+
class Solution:
11+
def maxDepth(self, root: Optional[TreeNode]) -> int:
12+
if root is None:
13+
return 0
14+
15+
max_depth = 1
16+
queue = [(root, 1)]
17+
18+
while queue:
19+
node, cur_depth = queue.pop()
20+
max_depth = max(cur_depth, max_depth)
21+
22+
if node.left:
23+
queue.append((node.left, cur_depth + 1))
24+
25+
if node.right:
26+
queue.append((node.right, cur_depth + 1))
27+
28+
return max_depth

0 commit comments

Comments
ย (0)