/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution
{
List<TreeNode> list1 = new List<TreeNode>();
List<TreeNode> list2 = new List<TreeNode>();
private void postTree(TreeNode root)
{
if (root != null)
{
if (root.left != null)
{
list1.Add(root.left);
postTree(root.left);
} if (root.right != null)
{
postTree(root.right);
}
//左叶子,是左子树并且是叶子 if (root.left == null && root.right == null)//叶子节点
{
//如何判断是左子树呢
list2.Add(root);
}
}
} public int SumOfLeftLeaves(TreeNode root)
{
postTree(root); var list = list1.Intersect(list2); var sum = ; foreach (var l in list)
{
sum += l.val;
} return sum;
}
}

https://leetcode.com/problems/sum-of-left-leaves/#/description

补充一个python的实现:

 class Solution:
def __init__(self):
self.leftsum =
def preOrder(self,root):
if root != None:
if root.left != None and root.left.left == None and root.left.right == None:
self.leftsum += root.left.val
self.preOrder(root.left)
self.preOrder(root.right) def sumOfLeftLeaves(self, root: TreeNode) -> int:
self.preOrder(root)
return self.leftsum

leetcode404的更多相关文章

  1. [Swift]LeetCode404. 左叶子之和 | 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. LeetCode404.左叶子之和

    题目 法一.广度优先搜索 1 class Solution { 2 public: 3 int sumOfLeftLeaves(TreeNode* root) { 4 if(root == NULL) ...

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

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

  4. leetcode_二叉树篇_python

    主要是深度遍历和层序遍历的递归和迭代写法. 另外注意:因为求深度可以从上到下去查 所以需要前序遍历(中左右),而高度只能从下到上去查,所以只能后序遍历(左右中). 所有题目首先考虑root否是空.有的 ...

  5. LeetCode通关:连刷三十九道二叉树,刷疯了!

    分门别类刷算法,坚持,进步! 刷题路线参考:https://github.com/youngyangyang04/leetcode-master 大家好,我是拿输出博客来督促自己刷题的老三,这一节我们 ...

随机推荐

  1. OJ链接

    BNU..好难找..http://www.bnuoj.com

  2. 《DSP using MATLAB》 Problem 3.19

    先求模拟信号经过采样后,对应的数字角频率: 明显看出,第3种采样出现假频了.DTFT是以2π为周期的,所以假频出现在10π-2kπ=0处. 代码: %% ----------------------- ...

  3. 《DSP using MATLAB》Problem 3.4

    代码: %% ------------------------------------------------------------------------ %% Output Info about ...

  4. windows dos命令

    dos命令配置环境变量: path=%path%;D:\Installed software\Professional software\Python27   (https://www.cnblogs ...

  5. pandas Timestamp的用法

    (Timestamp('2018-08-01 00:00:00'), <class 'pandas._libs.tslibs.timestamps.Timestamp'>) 注意这里面的T ...

  6. 7 无线wifi传输视频开发

    转载,侵删 7 无线wifi传输视频开发 MT7601的驱动源码提供了两种:AP模式和STA模式源码.此时我使用USB作为AP热点,电脑作为STA模式,并使用ORTP实现无线传输视频 7.1.AP模式 ...

  7. C语言面试题4

    第二部分:程序代码评价或者找错 1.下面的代码输出是什么,为什么?void foo(void){    unsigned int a = 6;    int b = -20;    (a+b > ...

  8. post请求(headers里有属性)报错:Request header field xxx is not allowed by Access-Control-Allow-Headers in preflight response

    post 请求,headers里有属性(xxx).请求时报错: XMLHttpRequest cannot load <url>. Request header field xxx is ...

  9. php实现Facebook风格的 time ago函数

    php实现Facebook风格的 time ago函数 非常好用,只要把里面的英文替换成中文就行了 英文函数代码如下: <?php function nicetime($date) { if(e ...

  10. Angular 4 父组件调用子组件中的方法

    1. 创建工程 ng new demo3 2. 创建子组件 ng g component child 3. 在子组件中定义方法greeting 4. 父组件html(第三行是模板中调用子组件的方法) ...