[抄题]:

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只是递归的过程,必须要真正到了叶子节点才能求和

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 左子树第一步先检验是否到了叶子节点,类似于特殊判断的思想,涨经验
  2. 在全树求和的题要+= 才行,不是递归一个算式就完了

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼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 左叶子之和的更多相关文章

  1. [leetcode]404. Sum of Left Leaves左叶子之和

    弄个flag记录是不是左节点就行 int res = 0; public int sumOfLeftLeaves(TreeNode root) { if (root==null) return res ...

  2. LeetCode404Sum of Left Leaves左叶子之和

    计算给定二叉树的所有左叶子之和. 示例: 3 / \ 9    20 / \ 15   7 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24 class Solution { pub ...

  3. [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 ...

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

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

  5. [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 ...

  6. [LeetCode]404. 左叶子之和(递归)、938. 二叉搜索树的范围和(递归)(BST)

    题目 404. 左叶子之和 如题 题解 类似树的遍历的递归 注意一定要是叶子结点 代码 class Solution { public int sumOfLeftLeaves(TreeNode roo ...

  7. 【LeetCode】404. 左叶子之和

    404. 左叶子之和 知识点:二叉树 题目描述 计算给定二叉树的所有左叶子之和.. 示例 3 / \ 9 20 / \ 15 7 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24 解 ...

  8. Java实现 LeetCode 404 左叶子之和

    404. 左叶子之和 计算给定二叉树的所有左叶子之和. 示例: 3 / \ 9 20 / \ 15 7 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24 /** * Definiti ...

  9. 左叶子之和(sum-of-left-leaves)

    LeetCode题目--左叶子之和(sum-of-left-leaves) 计算给定二叉树的所有左叶子之和. 示例: 3 / \ 9 20 / \ 15 7 在这个二叉树中,有两个左叶子,分别是 9 ...

随机推荐

  1. python 有关引用的一些问题

    python 有关引用的一些问题 print id.__doc__ ​ id(object) -> integer Return the identity of an object. This ...

  2. 将socket程序从linux移植到windows上

    今天突然想试下纯socket编程在两个系统上代码重合量有多大,只要不使用VC自定义的宏(比如SOCKET.SOCKADDR等等)感觉代码重合量挺大的. 比如最简单的TCP客户端和服务端对话,在VC中用 ...

  3. span 右浮动折行 解决ie6/7中span右浮动折行问题

    RM8005: IE6 IE7 IE8(Q) 中行内元素后相邻的浮动元素在某些情况下会折行放置在之前行内元素所在行框的底部 标准参考 W3C CSS 2.1 规范文档里对于浮动元素与非浮动行内元素相邻 ...

  4. 029:高可用之MHA

    高可用之MHA 一.MHA 简介 MHA(Master High Availability)是一套优秀的作为MySQL高可用性环境下故障切换和主从提升的高可用软件.在MySQL故障切换过程中,MHA能 ...

  5. 软件官网与memcached介绍

    常见官网 http://www.keepalived.org http://nginx.org/ documentation 模块说明 http://www.apache.org/ https://o ...

  6. Ajax显示隐藏

    $(function(){ $('#search').click(function(){ if($(".search_div").is(":visible")) ...

  7. web页面取用户控件页面中服务器控件的值

    用户控件页面后台: public string P_Name { get { return txt_P_name.Value; } set { txt_P_name.Value = value; } ...

  8. php网站环境无法上传的解决办法?

    一. 检查网站目录的权限.二. php.ini配置文件php.ini中影响上传的有以下几处:file_uploads 是否开启 on 必须开启是否允许HTTP文件上传post_max_size = 8 ...

  9. MySql——查询题目练习

    本次查询我们基于这几张表查询 ***********查询练习********** 1. 查询Student表中的所有记录的Sname.Ssex和Class列. select sname,ssex,cl ...

  10. TOTAL COMMAND自定义快捷键