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. 浏览器XMLHttpRequest案例

    /* Cross-Browser XMLHttpRequest v1.2 ================================= Emulate Gecko 'XMLHttpRequest ...

  2. C#-数据库访问技术 ado.net——创建 数据库连接类 与 数据库操作方法 以及简单的数据的添加、删除、修改、查看

    数据库访问技术 ado.net 将数据库中的数据,提取到内存中,展示给用户看还可以将内存中的数据写入数据库中去 并不是唯一的数据库访问技术,但是它是最底层的数据库访问技术 1.创建数据库,并设置主外键 ...

  3. python3.x + selenium 3.x 遇到的问题:Exception ignored in: <bound method Service.__del__ of <selenium.webdriver.firefox.service.Service object at 0x0045E450>>

    我解决了!!!from selenium import webdriverimport timedr = webdriver.Firefox(executable_path = '/Users/jin ...

  4. 安装Win7提示Windows无法安装到磁盘怎么办

    Windows之家(www.windowszj.com):在安装Win7系统的过程中,由于每台电脑的状态不一样,比如硬件配置原因,或者是硬盘格式.硬盘状态等问题,会使得每台电脑在安装过程中都会有些不一 ...

  5. 从CIO、CEO、CFO、COO...到CVO 这22个你了解几个? (史上最完整版)

    1.CEO:是Chief Executive Officer的缩写,即首席执行官. 由于市场风云变幻,决策的速度和执行的力度比以往任何时候都更加重要.传统的“董事会决策.经理层执行”的公司体制已经难以 ...

  6. bzoj4400: tjoi2012 桥

    先传代码再填坑 #include <iostream> #include <cstdio> #include <cmath> #include <cstrin ...

  7. android开发之生命周期

    android开发之生命周期 一:Activity的生命周期: 这几天了了解了安卓Activity的生命周期,对于生命周期有了大概的理解: 一个Activity的生命周期也就是Activity从生成到 ...

  8. linux中/和/root(~) 和 /home

    winodws是森林型目录结构,它有很多根,如C.D.E.F等都是它的根目录,然后在其实创建子目录linux是树型目录结构,它只有一个根就是/目录,然后在/目录在有子目录如/root./home./e ...

  9. Hash_集合

    #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> ...

  10. [NOIP2013] 提高组 洛谷P1969 积木大赛

    题目描述 春春幼儿园举办了一年一度的“积木大赛”.今年比赛的内容是搭建一座宽度为n的大厦,大厦可以看成由n块宽度为1的积木组成,第i块积木的最终高度需要是hi. 在搭建开始之前,没有任何积木(可以看成 ...