소프트웨어에 대한 모든 것

LeetCode 풀기 - 104. Maximum Depth of Binary Tree 본문

알고리즘/LeetCode

LeetCode 풀기 - 104. Maximum Depth of Binary Tree

앤테바 2022. 2. 15. 08:32
반응형

104. Maximum Depth of Binary Tree

https://leetcode.com/problems/maximum-depth-of-binary-tree/

 

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

문제)

솔루션1) DFS

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def maxDepth(self, root: Optional[TreeNode]) -> int:     
        def recur(node, depth):
            if node is None:
                return depth            
            return max(recur(node.left, depth+1), recur(node.right, depth+1))        
        return recur(root, 0)

 

반응형
Comments