1 class Solution: 2 def levelOrder(self,root,l,res): 3 while len(l) > 0: 4 temp = [] 5 while len(l) > 0: 6 cur = l.pop() 7 if cur.left != None: 8 temp.append(cur.left) 9 if cur.right != None: 10 temp.append(cur.right) 11 if len(temp) > 0: 12 l = temp[:] 13 res.clear() 14 for t in range(len(temp)): 15 res.append(temp[t].val) 16 17 18 def deepestLeavesSum(self, root: 'TreeNode') -> int: 19 if root == None: 20 return 0 21 l = [root] 22 res = [root.val] 23 self.levelOrder(root,l,res) 24 sums = 0 25 for i in range(len(res)): 26 sums += res[i] 27 return sums
算法思想:二叉树层次遍历。