題目來源:
?
自我感覺難度/真實難度:
?
題意:
?
分析:
?
自己的代碼:
class Solution(object):def sumOfLeftLeaves(self, root):""":type root: TreeNode:rtype: int"""left=[]if not root:returnself.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 = 0if not root:return 0 if root.left and not root.left.left and not root.left.right:result += root.left.valreturn result+self.sumOfLeftLeaves(root.left)+self.sumOfLeftLeaves(root.right)
?
代碼效率/結果:
?36ms
自己優化后的代碼:
?
反思改進策略:
1.樹可以這樣操作root.left.val
2.迭代要記住返回值是什么,想想最簡單的情況
3.迭代可以出現在return中
?