104. Maximum Depth of Binary Tree
· 약 1분
[Maximum Depth of Binary Tree - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com](https://leetcode.com/problems/maximum-depth-of-binary-tree/)
1. DFS를 이용한 내 풀이
class Solution:
answer = 0
def maxDepth(self, root: TreeNode) -> int:
def dfs(cur, depth):
self.answer = max(self.answer, depth)
if cur.left:
dfs(cur.left, depth + 1)
if cur.right:
dfs(cur.right, depth + 1)
if not root:
return 0
dfs(root, 0)
return self.answer+1

2. 직관적이고 섹시한 풀이
class Solution:
def maxDepth(self, root: TreeNode) -> int:
if not root:
return 0
return 1 + max(self.maxDepth(root.right), self.maxDepth(root.left))
