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 ...
随机推荐
- Linux网络管理2---(网络环境查看命令、网络测试命令)
1.网络环境查看命令 ifconfig命令 查看或临时修改网络状态的命令 可以看到IP.子网掩码……信息 关闭和启动网卡 ifdown 网卡设备名(比如: ifdown eth0):禁用该网卡设备 i ...
- Linux TOP 交互命令
今天总结一点top命令的一些交互命令,比较实用! h或者? 显示帮助画面,给出一些简短的命令总结说明 k 终止一个进程. 系统将提示用户输 ...
- Knockout.js随手记(4)
动态绑定下拉列表 在<select> data-bind的options选项如果绑定到ko.observableArray(),就可以动态新增选项效果,也就是可以利用其完成常见的级联效果的 ...
- notepad++ 中配置 python一键运行
cmd /k python "$(FULL_CURRENT_PATH)"& PAUSE & EXIT cmd /k python 表示打开Command Promp ...
- Struts2 动态方法调用
01.Struts 2基本结构 使用Struts2框架实现用登录的功能,使用struts2标签和ognl表达式简化了试图的开发,并且利用struts2提供的特性对输入的数据进行验证,以及访问Servl ...
- 微信开发笔记:公众号获取access_token
微信开发中,access_token的获取是一种非常常见的功能,通过公众号的appid和appsecret来向微信公众平台请求一个临时通行凭证:access_token.公众平台上的绝大部分操作都会需 ...
- 与IE奋战的血泪史
IE6下font-size会撑高元素,也就是说IE6下元素的最小高度为font-size的高度(蛋疼) IE6不支持两个class 例如 .a.b,类名不支持下划线开头 通过js设置样式带下划线的样式 ...
- 【游戏开发】小白学Lua(上)
在很多游戏中,脚本语言是不可或缺的一部分,很多游戏都使用到了Lua,js,python一类的脚本,脚本语言可以在很多方面给开发进程带来帮助.脚本语言可以作为初始化文件读入变量和游戏数据的一个快速而方便 ...
- 怎样去除织梦版权信息中的Power by DedeCms
用织梦建站时,网站底部调用的版权信息最后总会多出一个Power by DedeCms链接,此链接是织梦系统中默认的指向织梦官网的外链.本文就介绍两种去除这个外链的方法. 1.为什么要去除Power b ...
- 【noip 2004】 合并果子
noip2016结束后的第一份代码--优先队列的练习 合并果子 原题在这里 #include <iostream> #include <queue> #include < ...