力扣404. 左叶子之和

原题链接

 1 class Solution:
 2     ans = 0
 3     def sumOfLeftLeaves(self, root: TreeNode) -> int:
 4         def dfs(root,flag):
 5             if not root:return
 6             if not root.left and not root.right and flag:
 7                 self.ans += root.val
 8             dfs(root.left, True)
 9             dfs(root.right, False)
10         dfs(root,False)
11         return self.ans
12         
13             

 

posted @ 2021-01-24 16:11  凝视深空  阅读(29)  评论(0编辑  收藏  举报