알고리즘 문제/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)