Sum of Left Leaves

    这道题为简单题

  题目:

    

  思路:

    利用广搜,对每个节点进行判断,是否它的左儿子存在,并且它左儿子的左右儿子都不存在,那么total就加上它左儿子的值

  代码:

 1 class Solution(object):
 2     def sumOfLeftLeaves(self, root):
 3         """
 4         :type root: TreeNode
 5         :rtype: int
 6         """
 7         if not root: return 0
 8         a = [root]
 9         total = 0
10         while len(a) > 0:
11             node = a[0]
12             if node.left:
13                 a.append(node.left)
14                 if not node.left.left and not node.left.right:
15                     total += node.left.val
16             if node.right:
17                 a.append(node.right)
18             a.pop(0)
19         return total

 

posted @ 2017-09-07 15:05  唐僧洗发爱飘柔  阅读(106)  评论(0)    收藏  举报