【leetcode】404. Sum of Left Leaves【E】

来源:互联网 发布:985贴吧 知乎 编辑:程序博客网 时间:2024/05/16 12:30

Find the sum of all left leaves in a given binary tree.

Example:

    3   / \  9  20    /  \   15   7There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.

Subscribe to see which companies asked this question

这是一道找不到的题,哈哈

就是对每个节点,判断其是不是左节点+叶子节点

递归进行


# Definition for a binary tree node.# class TreeNode(object):#     def __init__(self, x):#         self.val = x#         self.left = None#         self.right = Noneclass Solution(object):    def isLeaf(self,root):        if root.left == None and root.right == None:            return True            def sumOfLeftLeaves(self, root):        if root == None:            return 0        res = 0         #print root.val        if root.left and self.isLeaf(root.left):            res += root.left.val                    res += self.sumOfLeftLeaves(root.left) + self.sumOfLeftLeaves(root.right)                return res        """        :type root: TreeNode        :rtype: int        """


0 0
原创粉丝点击