404. Sum of Left Leaves
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的更多相关文章
- 【Leetcode】404. Sum of Left Leaves
404. Sum of Left Leaves [题目]中文版 英文版 /** * Definition for a binary tree node. * struct TreeNode { * ...
- 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 ...
- 16. leetcode 404. Sum of Left Leaves
Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 ...
- 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 ...
- [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 ...
- 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 ...
- 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 ...
- [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 ...
- 【LeetCode】404. Sum of Left Leaves 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目大意 题目大意 解题方法 递归 迭代 日期 [LeetCode] 题目地址:h ...
随机推荐
- 将代码托管到OSChina服务器上
前言:前面的文章已经介绍过了如何将代码托管到github的服务器.并且笔者也详细的介绍了使用HTTPS和SSH两种不同的身份验证方式.但是仅仅介绍github的使用是不够的,原因笔者已经在前面的文章中 ...
- Linux 输出重定向>和>>的区别
>是定向输出到文件,如果文件不存在,就创建文件:如果文件存在,就将其清空:一般我们备份清理日志文件的时候,就是这种方法:先备份日志,再用`>`,将日志文件清空(文件大小变成0字节): &g ...
- 【poj2478】 Farey Sequence
http://poj.org/problem?id=2478 (题目链接) 题意 求分母小于等于n的真分数的个数. Solution 现在只能做做水题了,唉,思维僵化. 细节 前缀和开LL 代码 // ...
- POJ3273 Monthly Expense
查看原题 边界,就是边界和思维,怎么有效的判断中间值是大了还是小了,以及准确的找到边界!一个<写成<=就前功尽弃,还特别难找到错误! #include <cstdio> #in ...
- RabbitMQ之前的那些事
RabbitMQ消息队列 RabbitMQ是一个消息队列的产品有着 集群.消息确认.内存化.高可用.镜像等高级功能,是目前MQ产品中的佼佼者 RabbitMQ的来历 它是用erlang语言遵守amqp ...
- SQL数据操作和查询
1.Oracle中可以把查询的结果根据结果集中的表结构和数据形成一张新表. CREATE TABLE 表名 AS SELECT语句,例如: CREATE TABLE INFOS1 AS SELECT ...
- CMD命令之 :修改windows的CMD窗口输出编码格式为UTF-8
修改windows的CMD窗口输出编码格式为UTF-8 转载自 http://xuliduo.iteye.com/blog/639923 dos命令: chcp 65001 就是换成UTF-8代码页 ...
- centos6.5下安装mysql
http://www.centoscn.com/mysql/2014/0812/3481.html 1.使用yum命令安装mysql [root@bogon ~]# yum -y install m ...
- javascript表单验证
表单HTML <form action="" method="post"> <fieldset class="login" ...
- DNS(二)之构建域名解析缓存
域名解析缓存的必要性 在部署服务的时候,很多程序需要使用域名解析的功能,一般配置/etc/resovl.conf去指定DNS服务器的IP,但是如果程序发起的请求量较大,那么服务器就容易被DNS服务器禁 ...