首页 > 代码库 > 【leetcode?python】 Sum of Left Leaves

【leetcode?python】 Sum of Left Leaves

#-*- coding: UTF-8 -*-
# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    
    def dfs(self,root,isLeft):
        
        if root==None:return 0
        if(root.left==None and root.right==None and isLeft==True):return root.val
        return self.dfs(root.left,True)+self.dfs(root.right,False)
    
    def sumOfLeftLeaves(self, root):
        return self.dfs(root,False)
       

【leetcode?python】 Sum of Left Leaves