404. 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 left leaves in the binary tree, with values 9 and 15 respectively. Return 24.
[暴力解法]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
root.left root.right只是递归的过程,必须要真正到了叶子节点才能求和
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 左子树第一步先检验是否到了叶子节点,类似于特殊判断的思想,涨经验
- 在全树求和的题要+= 才行,不是递归一个算式就完了
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
root.left root.right只是递归的过程,必须要真正到了叶子节点才能求和
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int sumOfLeftLeaves(TreeNode root) {
//corner case
if (root == null) {
return 0;
}
int sum = 0;
//left
if (root.left != null) {
if (root.left.left == null && root.left.right == null) {
sum += root.left.val;
}else {
sum += sumOfLeftLeaves(root.left);
}
}
//right
sum += sumOfLeftLeaves(root.right);
//return
return sum;
}
}
404. Sum of Left Leaves 左叶子之和的更多相关文章
- [leetcode]404. Sum of Left Leaves左叶子之和
弄个flag记录是不是左节点就行 int res = 0; public int sumOfLeftLeaves(TreeNode root) { if (root==null) return res ...
- LeetCode404Sum of Left Leaves左叶子之和
计算给定二叉树的所有左叶子之和. 示例: 3 / \ 9 20 / \ 15 7 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24 class Solution { pub ...
- [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 404. 左叶子之和(Sum of Left Leaves)
404. 左叶子之和 404. Sum of Left Leaves LeetCode404. Sum of Left Leaves 题目描述 计算给定二叉树的所有左叶子之和. 示例: 3 / \ 9 ...
- [Swift]LeetCode404. 左叶子之和 | 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]404. 左叶子之和(递归)、938. 二叉搜索树的范围和(递归)(BST)
题目 404. 左叶子之和 如题 题解 类似树的遍历的递归 注意一定要是叶子结点 代码 class Solution { public int sumOfLeftLeaves(TreeNode roo ...
- 【LeetCode】404. 左叶子之和
404. 左叶子之和 知识点:二叉树 题目描述 计算给定二叉树的所有左叶子之和.. 示例 3 / \ 9 20 / \ 15 7 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24 解 ...
- Java实现 LeetCode 404 左叶子之和
404. 左叶子之和 计算给定二叉树的所有左叶子之和. 示例: 3 / \ 9 20 / \ 15 7 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24 /** * Definiti ...
- 左叶子之和(sum-of-left-leaves)
LeetCode题目--左叶子之和(sum-of-left-leaves) 计算给定二叉树的所有左叶子之和. 示例: 3 / \ 9 20 / \ 15 7 在这个二叉树中,有两个左叶子,分别是 9 ...
随机推荐
- Java API 操作Zookeeper
一.依赖 <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookee ...
- c# 数据库通用类DbUtility
DbProviderType数据库类型枚举 /// <summary> /// 数据库类型枚举 /// </summary> public enum DbProviderTyp ...
- 常用 Git 命令使用教程
下面整理一下自己在开发过程中经常使用到的 Git 命令.使用 GUI 工具的同学,也可以对照起来看看. Git 配置 1. 在安装完成 Git 后,开始正式使用前,是需要有一些全局设置的,如用户名.邮 ...
- Error[Li006]: duplicate definitions for "******"
今天参考别人程序写程序时出现Error[Li006]: duplicate definitions for "******". 参考程序中将变量和数据定义在(.h)文件中,我也就直 ...
- 在Mac OS上搭建Python的开发环境
本文转载自:http://www.jb51.net/article/76931.htm 一. 安装python mac系统其实自带了一个python的执行执行环境,用来运行python还行,但是开发可 ...
- Appium ios新的定位方式FindsByIosNSPredicate (没有试 先记录在这里) 有个 driver.find_element_by_ios_uiautomation() 研究下 ios的定位
这个定位方式需要用java-client -5.0.版本,4.x的版本没有这个定位方式 //输入账号和密码 driver.findElementByIosNsPredicate("value ...
- JavaScript快速切换繁体中文和简体中文的方法及网站支持简繁体切换的绝招
一般商业网站都有一个语言的需求,就是为了照顾使用正体中文的国人,会特地提供一个切换到正体中文的选项(或曰“繁体中文”).传统做法是在服务端完成的,即通过某些控件或者过滤器转换文本语言.这里笔者介绍一种 ...
- HTML4 And HTML5 标签整理
一.文字备忘之标签 HTML5中新增的标签<article> 定义文章 <aside> 定义页面内容旁边的内容 <audio> ...
- linux命令日志处理
刘超 2018/10/8 10:32:43 zcat bi_www_activity_2018100*.log.gz |grep --color '多方电话h5_' | awk -F'|' '{pri ...
- 在Android Studio 0.5.2中使用ArcGIS Android SDK
环境 操作系统:Mac OSX 10.8.5Android Studio: 0.5.2ArcGIS Android SDK: 10.2.3 操作步骤 在Android Studio中新建一个Modul ...