Leetcode
[LeetCode] Sum of Left Leaves 파이썬 (Tree)
YGSEO
2021. 4. 15. 03:28
728x90
class Solution(object):
def sumOfLeftLeaves(self, root):
"""
:type root: TreeNode
:rtype: int
"""
def sumOfLeftLeavesHelper(root, is_left):
if not root:
return 0
if not root.left and not root.right:
return root.val if is_left else 0
return sumOfLeftLeavesHelper(root.left, True) + sumOfLeftLeavesHelper(root.right, False)
return sumOfLeftLeavesHelper(root, False)
leaf 노드 까지 탐색한다.
left일 경우에만 +
출처: github.com/jiapengwen/LeetCode/blob/master/Python/sum-of-left-leaves.py
728x90