leetcode?python Sum of Left Leaves

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的主要内容,如果未能解决你的问题,请参考以下文章

[LeetCode in Python] 5403 (H) find the kth smallest sum of a matrix with sorted rows 有序矩阵中的第 k 个最小数组

#Leetcode# 404. Sum of Left Leaves

LeetCode Sum of Square Numbers

Leetcode 404. Sum of Left Leaves

LeetCode 813. Largest Sum of Averages

LeetCode 404. Sum of Left Leaves