标签:bsp code val root pen 最简 int result not
class Solution(object): def sumOfLeftLeaves(self, root): """ :type root: TreeNode :rtype: int """ left=[] if not root: return self.sumOfLeftLeaves(root.left) left.append(root.val) self.sumOfLeftLeaves(root.right) return sum(left)
class Solution: def sumOfLeftLeaves(self, root): """ :type root: TreeNode :rtype: int """ result = 0 if not root: return 0 if root.left and not root.left.left and not root.left.right: result += root.left.val return result+self.sumOfLeftLeaves(root.left)+self.sumOfLeftLeaves(root.right)
1.树可以这样操作root.left.val
2.迭代要记住返回值是什么,想想最简单的情况
3.迭代可以出现在return中
标签:bsp code val root pen 最简 int result not
原文地址:https://www.cnblogs.com/captain-dl/p/10264184.html