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相关推荐
- 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 ...
- Java描述 LeetCode,404. Sum of Left Leaves 左叶子的节点总和
大家好,我是河海哥,专注于后端,如果可以的话,想做一名code designer而不是普通的coder,一起见证河海哥的成长,您的评论与赞是我的最大动力,如有错误还请不吝赐教,万分感谢.一起支持原创吧 ...
- 404. Sum of Left Leaves 左叶子之和
计算给定二叉树的所有左叶子之和. 示例: 3/ \9 20/ \15 7在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24 DFS 判断一下,然后要么加和要么继续DFS. Code d ...
- LeetCode 404. 左叶子之和(Sum of Left Leaves)
404. 左叶子之和 404. Sum of Left Leaves LeetCode404. Sum of Left Leaves 题目描述 计算给定二叉树的所有左叶子之和. 示例: 3/ \9 2 ...
- [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 ...
- 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 ...
- C#LeetCode刷题之#404-左叶子之和(Sum of Left Leaves)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4084 访问. 计算给定二叉树的所有左叶子之和. 3 / ...
- 递归算法经典实例_掌握这10道经典面试算法题(含答案),攻克递归算法【程序员必备】...
1. 树的高度 2. 平衡树 3. 两节点的最长路径 4. 翻转树 5. 归并两棵树 6. 判断路径和是否等于一个数 7. 统计路径和等于一个数的路径数量 8. 子树 9. 树的对称 10. 最小路径 ...
- Python JAVA Solutions for Leetcode
Python & JAVA Solutions for Leetcode (inspired by haoel's leetcode) Remember solutions are only ...
最新文章
- Docker入门六部曲——服务
- leangoo领歌看板工具新增任务到期提醒功能
- 函数重载和 函数模板
- phoenix的元数据一般存在哪里_Phoenix常用操作记录-阿里云开发者社区
- Tensorflow:在Tensorflow的不同版本中如何实现Xavier参数权重初始化
- Nginx图片剪裁模块探究 http_image_filter_module
- vfork()系统调用
- 去中心化交易协议 Bancor 新增支持限价订单
- python到底是啥_Python语言中的__init__到底是干什么的?
- 小技巧:Go怎么样获取常驻内存子进程的输出
- Django 模板标签[转]
- hive order by sort by distribute by总结
- 如何优雅地使用迅雷下载百度云大文件?
- 物联网技术-RFID
- 每日小结(就不平衡问题探讨)
- 网站域名服务器怎么备案,网站域名服务器怎么备案
- 10000marker_AL10000 DNA Marker使用说明书
- Rust 有问有答之 crate 是什么
- Oracle 查询当前系统时间的几种方式
- 微信小程序wxml和wxss样式