/**
* 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. hdu1301 Jungle Roads 最小生成树

    The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was s ...

  2. Servlet基本操作

    一.Servlet的请求流程 web项目中的web.xml文件配置为: <servlet> <!--别名--> <servlet-name>Hello</se ...

  3. sql 分组后显示每组的前几条记录

    sql 分组后显示每组的前几条记录 如   表中记录是             code       serialno             A1               1           ...

  4. FastAdmin 绑定的模块禁用路由

    为了安全,将后台入口隐藏. 这里出一个问题,因为装了 CMS 插件,使用入口登录后显示的是 CMS 的首页. 这个问题已经修复. https://gitee.com/karson/fastadmin/ ...

  5. VSCODE includePath 中使用系统中的变量

    使用 ${env.ENVNAME}  这样只需要在 系统中加一个系统变量就可以. https://github.com/Microsoft/vscode-cpptools/issues/697

  6. java中使用MD5加密的算法

    MD5,全名Message Digest Algorithm 5,中文名为消息摘要算法第五版,为计算机安全领域广泛使用的一种散列函数,用以提供消息的完整性保护.以下是JAVA语言中使用MD5加密的工具 ...

  7. git add -A、git add -u、git add .区别

    git add各命令及缩写 git add各命令 缩写 git add --all git add -A git add --update git add -u git add . Git Versi ...

  8. base64编码的原理及实现

    base64编码的原理及实现 我们的图片大部分都是可以转换成base64编码的data:image. 这个在将canvas保存为img的时候尤其有用.虽然除ie外,大部分现代浏览器都已经支持原生的基于 ...

  9. Vue 网络请求

    Vue网络请求,用的是vue-resource 1. 首先需要安装vue-resource npm install vue-resource 2. 安装好之后,会在package.json文件中自动加 ...

  10. ASP 三十二条精华代码 (1)

    ASP 三十二条精华代码 (1) 2009-08-10 09:53:03  www.hackbase.com  来源:互联网 1. oncontextmenu="window.event.r ...