Find the sum of all left leaves in a given binary tree.

左树的值(9+15=24)

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int sumOfLeftLeaves(TreeNode root) {
if(root==null){
return 0;
};
int ans=0;
Stack<TreeNode> stack=new Stack<TreeNode>();
stack.push(root); while(!stack.empty()){
TreeNode node=stack.pop(); if(node.left!=null&&node.left.left==null&&node.left.right==null){ ans+=node.left.val; }
if(node.right!=null){ stack.push(node.right);
}
if(node.left!=null){ stack.push(node.left);
} } return ans; }
}

404. Sum of Left Leaves的更多相关文章

  1. 【Leetcode】404. Sum of Left Leaves

    404. Sum of Left Leaves [题目]中文版  英文版 /** * Definition for a binary tree node. * struct TreeNode { * ...

  2. LeetCode - 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 l ...

  3. 16. leetcode 404. Sum of Left Leaves

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

  4. LeetCode 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 l ...

  5. [LeetCode&Python] Problem 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 l ...

  6. LeetCode 404. Sum of Left Leaves (C++)

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

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

  8. [LC] 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 l ...

  9. 【LeetCode】404. Sum of Left Leaves 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目大意 题目大意 解题方法 递归 迭代 日期 [LeetCode] 题目地址:h ...

随机推荐

  1. javac 编译与 JIT 编译

    编译过程 不论是物理机还是虚拟机,大部分的程序代码从开始编译到最终转化成物理机的目标代码或虚拟机能执行的指令集之前,都会按照如下图所示的各个步骤进行: 其中绿色的模块可以选择性实现.很容易看出,上图中 ...

  2. 【Codeforces 722C】Destroying Array (数据结构、set)

    题意 输入一个含有 n(1≤n≤100000) 个非负整数的 a 数组和一个 1-n 的排列 p 数组,求每次删除 a[p[i]] 后,最大连续子段和(不能跨越被删除的)是多少? 分析 因为都是非负整 ...

  3. extract()函数

    extract(),它的主要作用是将数组展开,键名作为变量名,元素值为变量值 extract($_POST);//相当于$username = $_POST['username'];//$passwo ...

  4. java.sql.SQLException: 无效的列索引

    java.sql.SQLException: 无效的列索引 "无效的列索引"其实是个低级的错误,原因无非几个: 1.sql串的?号数目和提供的变量数目不一致: 例如:jdbcTem ...

  5. the comment lines of the blast tabular format

    进行Blast比对,用参数-m 6 可以以列表的方式输出结果,结果中从左到右每一列的意义分别是: [00] Query id [01] Subject id [02] % identity [03] ...

  6. 【BZOJ-1068】压缩 区间DP

    1068: [SCOI2007]压缩 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 1001  Solved: 615[Submit][Status][ ...

  7. 【poj3261】 Milk Patterns

    http://poj.org/problem?id=3261 (题目链接) 题意 给出n个数和k,求在给出的数中,最长的出现至少k次的可重叠子串. solution 后缀数组论文题,感觉分组思想可能会 ...

  8. C语言之流的重定向

    写c的小程序断不了需要输入输出,手动输入可太麻烦了.下面介绍IO的重定向方式: .重定向标准输入输出和错误,直接在命令行使用符号< > > >> >>等,还可 ...

  9. Linux_LVM_磁盘扩容

    场景描述: 安装操作系统的时候,做了LVM,应用软件基本装在了“/”目录下,服务器运行一段时间后,该目录下的存储空间使用紧张,现利用LVM对其进行磁盘空间扩容. 注:安装系统的时候需要做逻辑卷管理,保 ...

  10. dedecms /member/buy_action.php Weak Password Vulnerability Algorithm Vul

    catalog . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 . 漏洞由mchStrCode函数弱算法(异或算法: 得其中2知余下1) ...