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 ...
随机推荐
- 用Eclipse进行java学习的步骤
用Eclipse进行java学习的步骤(1)File,new,File Project->在New Java Project页面的Project name文本框中填入名称,点击finish(2) ...
- guaua学习,工具专题
Preconditions 1,http://www.cnblogs.com/peida/p/Guava_Preconditions.html 1 .checkArgument(boolean) : ...
- Linux 文件系统目录结构
进入 Linux 根目录(即 "/",Linux文件系统的入口,也是处于最高一级的目录),运行 "ls -l" 命令,可以看到 Linux 系统目录. 1./b ...
- AT指令(二)
1.常用操作1.1 AT命令解释:检测 Module 与串口是否连通,能否接收 AT 命令:命令格式:AT<CR>命令返回:OK (与串口通信正常) (无返回,与串 ...
- jQuery实现页面监听(某一个值发生改变时触发)
使用jQuery实现页面中监听 页面中某一个值(如input输入框)发生改变时触发. <html> <head> <title>RunJS</title& ...
- windows 我永远的最爱
我配置好这个真不容易.总结下,配置中没搞清楚发布远程日志网址的意思:每一个博客配置都不同,比如新浪.网易.51技术博客
- 1、Window.document对象
1.Window.document对象 一.找到元素: docunment.getElementById("id"):根据id找,最多找一个: var a =docunmen ...
- python中scipy学习——随机稀疏矩阵及操作
1.生成随机稀疏矩阵: scipy中生成随机稀疏矩阵的函数如下: scipy.sparse.rand(m,n,density,format,dtype,random_state) 1 参数介绍: 参数 ...
- socket编程之select()
int select(int maxfdp,fd_set *readfds,fd_set *writefds,fd_set *errorfds,struct timeval *timeout); 参数 ...
- Python - Pyinstaller
安装 Pyinstaller pip install pyinstaller 使用: test.py print("Hello World!") 命令行输入 pyinstaller ...