题目来源:
自我感觉难度/真实难度:
题意:
分析:
自己的代码:
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相关推荐

  1. LeetCode 404. Sum of Left Leaves

    题目: Find the sum of all left leaves in a given binary tree. Example: 3/ \9 20/ \15 7There are two le ...

  2. Java描述 LeetCode,404. Sum of Left Leaves 左叶子的节点总和

    大家好,我是河海哥,专注于后端,如果可以的话,想做一名code designer而不是普通的coder,一起见证河海哥的成长,您的评论与赞是我的最大动力,如有错误还请不吝赐教,万分感谢.一起支持原创吧 ...

  3. 404. Sum of Left Leaves 左叶子之和

    计算给定二叉树的所有左叶子之和. 示例: 3/ \9 20/ \15 7在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24 DFS 判断一下,然后要么加和要么继续DFS. Code d ...

  4. LeetCode 404. 左叶子之和(Sum of Left Leaves)

    404. 左叶子之和 404. Sum of Left Leaves LeetCode404. Sum of Left Leaves 题目描述 计算给定二叉树的所有左叶子之和. 示例: 3/ \9 2 ...

  5. [LeetCode] Sum of Left Leaves 左子叶之和

    Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...

  6. LeetCode之Sum of Left Leaves

    1.题目 Find the sum of all left leaves in a given binary tree. Example: 3/ \9 20/ \15 7There are two l ...

  7. C#LeetCode刷题之#404-左叶子之和​​​​​​​​​​​​​​(Sum of Left Leaves)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4084 访问. 计算给定二叉树的所有左叶子之和. 3      / ...

  8. 递归算法经典实例_掌握这10道经典面试算法题(含答案),攻克递归算法【程序员必备】...

    1. 树的高度 2. 平衡树 3. 两节点的最长路径 4. 翻转树 5. 归并两棵树 6. 判断路径和是否等于一个数 7. 统计路径和等于一个数的路径数量 8. 子树 9. 树的对称 10. 最小路径 ...

  9. Python JAVA Solutions for Leetcode

    Python & JAVA Solutions for Leetcode (inspired by haoel's leetcode) Remember solutions are only ...

最新文章

  1. Docker入门六部曲——服务
  2. leangoo领歌看板工具新增任务到期提醒功能
  3. 函数重载和 函数模板
  4. phoenix的元数据一般存在哪里_Phoenix常用操作记录-阿里云开发者社区
  5. Tensorflow:在Tensorflow的不同版本中如何实现Xavier参数权重初始化
  6. Nginx图片剪裁模块探究 http_image_filter_module
  7. vfork()系统调用
  8. 去中心化交易协议 Bancor 新增支持限价订单
  9. python到底是啥_Python语言中的__init__到底是干什么的?
  10. 小技巧:Go怎么样获取常驻内存子进程的输出
  11. Django 模板标签[转]
  12. hive order by sort by distribute by总结
  13. 如何优雅地使用迅雷下载百度云大文件?
  14. 物联网技术-RFID
  15. 每日小结(就不平衡问题探讨)
  16. 网站域名服务器怎么备案,网站域名服务器怎么备案
  17. 10000marker_AL10000 DNA Marker使用说明书
  18. Rust 有问有答之 crate 是什么
  19. Oracle 查询当前系统时间的几种方式
  20. 微信小程序wxml和wxss样式

热门文章

  1. [JS 分析] 天_眼_查 字体文件
  2. 砝码称重 洛谷 1441
  3. jQUery中closest和parents的主要区别是
  4. Android Sutido 编译速度优化
  5. EasyUI实现两个列表联动
  6. 数据库去重查询问题详解
  7. JavaScript实现自适应宽度的瀑布流
  8. github windows客户端
  9. 如何把一个java程序打包成exe文件,运行在没有java虚
  10. PostgreSQL 11 1Kw TPCC , 1亿 TPCB 7*24 强压耐久测试