日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

404. Sum of Left Leaves

發布時間:2023/11/29 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 404. Sum of Left Leaves 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目來源:
?
自我感覺難度/真實難度:
?
題意:
?
分析:
?
自己的代碼:
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中

?

轉載于:https://www.cnblogs.com/captain-dl/p/10264184.html

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的404. Sum of Left Leaves的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。