LeetCode Sum of Left Leaves
原题链接在这里:https://leetcode.com/problems/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.
题解:
DFS, 若root.left不为null时,检查root.left是否为leaf. 若是res+=root.left.val, 若不是继续DFS.
再从root.right做DFS.
Time Complexity: O(n), n是tree的node数目. Space: O(logn).
AC Java:
/**
* 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 res = 0;
if(root.left != null){
if(root.left.left == null && root.left.right == null){
res += root.left.val;
}else{
res += sumOfLeftLeaves(root.left);
}
} res += sumOfLeftLeaves(root.right); return res;
}
}
Iteration 做法.
Time Complexity: O(n). Space: O(logn).
AC Java:
/**
* 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 res = 0;
Stack<TreeNode> stk = new Stack<TreeNode>();
stk.push(root);
while(!stk.isEmpty()){
TreeNode cur = stk.pop();
if(cur.left != null){
if(cur.left.left == null && cur.left.right == null){
res += cur.left.val;
}else{
stk.push(cur.left);
}
}
if(cur.right != null){
stk.push(cur.right);
}
}
return res;
}
}
BFS也可以做.
Time Complexity: O(n). Space: O(n).
AC Java:
/**
* 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 res = 0;
LinkedList<TreeNode> que = new LinkedList<TreeNode>();
que.offer(root);
while(!que.isEmpty()){
TreeNode cur = que.poll();
if(cur.left != null){
if(cur.left.left == null && cur.left.right == null){
res += cur.left.val;
}else{
que.offer(cur.left);
}
}
if(cur.right != null){
que.offer(cur.right);
}
}
return res;
}
}
LeetCode Sum of Left Leaves的更多相关文章
- [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 ...
- 【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
404. Sum of Left Leaves Easy Find the sum of all left leaves in a given binary tree. Example: 3 / \ ...
- LeetCode——Sum of Two Integers
LeetCode--Sum of Two Integers Question Calculate the sum of two integers a and b, but you are not al ...
- 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】404. Sum of Left Leaves 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目大意 题目大意 解题方法 递归 迭代 日期 [LeetCode] 题目地址:h ...
- 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 ...
随机推荐
- codeigniter框架The URI you submitted has disallowed characters错误解决方法
CI中URI传递参数时,出现:The URI you submitted has disallowed characters.错误 .原因:这是由于uri中存在CI不允许的字符 . 解决办法:在con ...
- DB2 license过期的问题
今天启动DB2,无论如何都启动不了,报一个错误:“Windows 不能在 本地计算机 启动 DB2 - DB2COPY - DB2-0.有关更多信息,查阅系统事件日志.如果这是非 Microsoft ...
- shell腳本
1.awk过滤重复行 awk '!a[$0]++{print $0}' 过滤重复行 awk '!a[$1]++{print $1}' 过滤第一列重复行 并只打印第一列 awk '!($1 in a){ ...
- 【html+css】关于页面布局中遇到的问题记录
关于行内元素: 行内元素设置width无效, height无效(可以设置line-height), margin上下无效,padding上下无效,margin和padding可设置左右. text ...
- python安装第三方类库的方法
1.先到官网 http://pypi.python.org/pypi/setuptools 下载setuptools.exe文件并安装 点击 ez_setup.py进入, 并将内容复制下来, 保存为本 ...
- Linux下面桌面的安装
挂载光盘后配置yum文件,配置后 使用yum grouplist 命令 查看组 用yum install '"GNOM桌面组 输入 init 5 即可进入桌面模式 0 关机 3命令行级 ...
- MongoDB使用小结:一些不常见的经验分享
最近一年忙碌于数据处理相关的工作,跟MongoDB打交道极多,以下为实践过程中的Q&A,后续会不定期更新补充. 另有<MongoDB使用小结:一些常用操作分享>,注:本文完成时Mo ...
- About_PHP_函数
关于验证码的完善: //生成干扰线 $posLineX1 = rand(12,50); $posLineX2 = rand(50,110); $posX = rand(10,50); for($i=0 ...
- [转]Flash Player、AIR、Flex SDK 大全
平时不断看到有朋友在各种论坛.空间.知道.群里求 Flash 平台各种版本的运行时(Flash Player)和SDK(Flex.AIR).今天就看到不下10次!所以决定把 Macromedia.Ad ...
- linq To DataTable
//将IEnumerable<T>类型的集合转换为DataTable类型 1111 public static DataTable LINQToDataTable<T>(IEn ...