배고픈 개발자 이야기

[Python] MaximumDepthofBinaryTree 본문

알고리즘 문제/LEETCODE

[Python] MaximumDepthofBinaryTree

이융희 2021. 5. 6. 01:27
728x90

Easy난이도 Acceptence 68.5%의 문제

 

자료구조의 Binary Tree의 가장 깊은 Depth를 구하는 문제로 

아래의 getDepth 함수는 result 리스트에 리프노드의 depth의 집합으로 max값을 구하여 해결

 

class Solution(object):
    def getDepth(self, node, depth):
        result = []
        if node.left is not None:
            result.extend(self.getDepth(node.left, depth+1))
        if node.right is not None:
            result.extend(self.getDepth(node.right, depth+1))
        if node.left is None and node.right is None:
            result.extend([depth])
        return result

    def maxDepth(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if root is None:
            return 0
        output = self.getDepth(root, 1)
        return max(output)

'알고리즘 문제 > LEETCODE' 카테고리의 다른 글

[Python] MergeTwoSortedLists  (0) 2021.05.06
[Python] MaximumSubarray  (0) 2021.05.06
[Python] ClimbingStairs  (0) 2021.05.06
1009. Complement of Base 10 Integer (feat.PYTHON)  (0) 2019.09.01
771. Jewels and Stones (feat.PYTHON)  (0) 2019.09.01
Comments