Skip to content

Commit 4615d6b

Browse files
committed
solve maximum depth of binary tree
1 parent 2dccaf5 commit 4615d6b

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
class Solution:
8+
def maxDepth(self, root: Optional[TreeNode]) -> int:
9+
"""
10+
์‹œ๊ฐ„๋ณต์žก๋„: O(n)
11+
๊ณต๊ฐ„๋ณต์žก๋„: O(n)
12+
์ด์ง„ ํŠธ๋ฆฌ ํƒ์ƒ‰ํ•˜๋ฉฐ ๋ށ์Šค ์ฆ๊ฐ€ ๊ณ„์† ํ•˜๋ฉฐ ์ง„ํ–‰
13+
"""
14+
depth = 0
15+
if not root:
16+
return 0
17+
array = [(root, 1)]
18+
while array:
19+
node, d = array.pop()
20+
depth = max(d, depth)
21+
if node.left:
22+
array.append((node.left, d + 1))
23+
if node.right:
24+
array.append((node.right, d + 1))
25+
return depth

0 commit comments

Comments
ย (0)