Skip to content

Commit 54fcea1

Browse files
committed
maximum-depth-of-binary-tree solution
1 parent a3f69e6 commit 54fcea1

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
# DFS, 재귀
11+
def dfs(root):
12+
# 노드가 없으면 깊이 0
13+
if not root:
14+
return 0
15+
16+
# 왼쪽과 오른쪽 중 더 깊은 쪽 + 1 리턴
17+
return 1 + max(dfs(root.left), dfs(root.right))
18+
19+
return dfs(root)

0 commit comments

Comments
 (0)