오늘도 개발

52. Maximum Depth of Binary Tree(DFS) 본문

자료구조 & 알고리즘/Leetcode

52. Maximum Depth of Binary Tree(DFS)

Sueeeeeee 2022. 12. 15. 17:50

문제

이진트리의 루트를 받아 최대 깊이를 계산하시오.

Input: root = [3,9,20,null,null,15,7]
Output: 3

 

해결 방식

dfs 사용.

class Solution:
    def maxDepth(self, root: Optional[TreeNode]) -> int:
        if not root:
            return 0
        return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1