本文主要是介绍【leetcode】404. Sum of Left Leaves【E】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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 Truedef sumOfLeftLeaves(self, root):if root == None:return 0res = 0 #print root.valif root.left and self.isLeaf(root.left):res += root.left.valres += self.sumOfLeftLeaves(root.left) + self.sumOfLeftLeaves(root.right)return res""":type root: TreeNode:rtype: int"""
这篇关于【leetcode】404. Sum of Left Leaves【E】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!