原题链接在这里: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的更多相关文章

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

  2. LeetCode 404. 左叶子之和(Sum of Left Leaves)

    404. 左叶子之和 404. Sum of Left Leaves LeetCode404. Sum of Left Leaves 题目描述 计算给定二叉树的所有左叶子之和. 示例: 3 / \ 9 ...

  3. 【Leetcode】404. Sum of Left Leaves

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

  4. 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 / \ ...

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

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

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

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

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

  9. 16. leetcode 404. Sum of Left Leaves

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

随机推荐

  1. sqlserver数据以及日志文件的设置小结

    1.1:增加次数据文件 从SQL SERVER 2005开始,数据库不默认生成NDF数据文件,一般情况下有一个主数据文件(MDF)就够了,但是有些大型的数据库,由于信息很多,而且查询频繁,所以为了提高 ...

  2. 【Cocos2d-x for WP8 学习整理】(4)CCTableView 实现《天天爱消除》中的得分榜

    接上回 CCScrollView 继续,在GUI 里还有个 CCScrollView 的子类---CCTableView . 这个名字应该是从 IOS 里的 UITableView来的,其实是跟WP8 ...

  3. htaccess分布式配置文件常用写法

    htaccess 写法 Apache中的.htaccess(或者”分布式配置”了针对目录改变配置的方法,即,在特定的文档目录中放置包含或多个指令的,以作用于此目录及其子目录.作为,所能的命令受到限制. ...

  4. 解决Spring的Component-scan和packagesToScan不支持Eclipse RCP问题

    http://www.360doc.com/content/13/0401/13/10825198_275274565.shtml

  5. 数位DP BZOJ 1026 [SCOI2009]windy数

    题目链接 前面全是0的情况特判 #include <bits/stdc++.h> int dp[10][10]; int digit[10]; int DFS(int pos, int v ...

  6. Fast Fourier Transform

    写在前面的.. 感觉自己是应该学点新东西了.. 所以就挖个大坑,去学FFT了.. FFT是个啥? 挖个大坑,以后再补.. 推荐去看黑书<算法导论>,讲的很详细 例题选讲 1.UOJ #34 ...

  7. Struts2文件上传与下载

    一,页面 index.html 在页面中最重要的就是这个文件上传用的 form 表单,注意这里一定要把 form 的encyType属性明确标定为“multipart/form-data”,只有这样. ...

  8. js获取?后面具体参数的值

    function getURLParam(name) {         return decodeURIComponent((new RegExp('[?|&]' + name + '=' ...

  9. MongoDB基本使用

    成功启动MongoDB后,再打开一个命令行窗口输入mongo,就可以进行数据库的一些操作. 输入help可以看到基本操作命令: show dbs:显示数据库列表 show collections:显示 ...

  10. css 注意点

    HTML css 一.整体布局 1.创建一个html标签 2.创建三个div标签(分别是网页的头部,中间,和底部三部分) 3.一般都用class选择器 4.用css给body标签加个 margin:0 ...