文章目录
- Question
- Ideas
- Code
Question
Ideas
找叶子节点累加
Code
class Solution:
def sumOfLeftLeaves(self, root: TreeNode) -> int:
def leftLeaf(node):
if not node.left and not node.right:
self.ans += node.val
def DFS(root):
if root.left:
leftLeaf(root.left)
DFS(root.left)
if root.right:
DFS(root.right)
if not root: return 0
self.ans = 0
DFS(root)
return self.ans