LeetCode之404. Sum of Left Leaves

-------------------------------------------------------------------
分两种情况:
1.当前节点拥有左孩子并且左孩子是叶子节点:左孩子值+右孩子子树遍历统计
2.不符合上面那种情况的从当前节点劈开为两颗子树分别统计相加即可
AC代码:
/**
* 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;
else if(root.left!=null && root.left.left==null && root.left.right==null) return root.left.val+sumOfLeftLeaves(root.right);
else return sumOfLeftLeaves(root.left)+sumOfLeftLeaves(root.right);
}
}
题目来源: https://leetcode.com/problems/sum-of-left-leaves/
LeetCode之404. Sum of Left Leaves的更多相关文章
- 【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 解题报告(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 ...
- 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 ...
- [LeetCode&Python] Problem 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 (C++)
题目: Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are t ...
- [leetcode]404. Sum of Left Leaves左叶子之和
弄个flag记录是不是左节点就行 int res = 0; public int sumOfLeftLeaves(TreeNode root) { if (root==null) return res ...
- LeetCode算法题-Sum of Left Leaves(Java实现)
这是悦乐书的第217次更新,第230篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第85题(顺位题号是404).找到给定二叉树中所有左叶的总和.例如: 二叉树中有两个左叶 ...
随机推荐
- 【SQL】sql server 2008R2 评估期已过,
参考1:http://www.cnblogs.com 参考2:http://www.wang1314.com 个人认为:升级+秘钥,,买正版才是最终的解决方法.
- C++异常处理
引言 异常,让一个函数可以在发现自己无法处理的错误时抛出一个异常,希望它的调用者可以直接或者间接处理这个问题.而传统错误处理技术,检查到一个局部无法处理的问题时: 1.终止程序(例如atol,atoi ...
- 谈谈duilib
据我所知,duilib已经不再维护了,q群也不再对外开放了. 尽管不再更新,但duilib的学习价值还是很好的.当有一定基础后再扩展duilib的功能,或根据duilib学习到的知识去着手写自己的界面 ...
- React.js常识
1.React的特点: 简单.声明式-自动dom操作. React核心:组件,组件的设计目的:提高代码复用率,降低测试难度和代码复杂度. 提高代码复用率:组建将数据和逻辑进行封装,类似面向对象的类: ...
- bzoj1584
1584: [Usaco2009 Mar]Cleaning Up 打扫卫生 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 467 Solved: 31 ...
- C# 6.0 新特性
1.C# 6.0 示例 1: 自动属性支持初始化, 字符串嵌入的新方式, 通过 Using Static 引用静态类, nameof 表达式CSharp6/Demo1.xaml.cs /* * C# ...
- Angular指令渗透式理解
通过一段时间对angular指令的使用,理解了angular指令的意义,下面逐一介绍一下. ng-app:定义一个angualr模块,表示angular作用的范围,如下代码: ng-app在html标 ...
- Beta阶段第二次Scrum Meeting
此文章已于 2:51:42 2016/12/8 重新发布到 buaa_overwatch Beta阶段第二次Scrum Meeting 情况简述 BETA阶段第二次Scrum Meeting 敏捷开发 ...
- [转载]抓包,端口镜像,monitor session命令(转)
原文地址:抓包,端口镜像,monitor session命令(转)作者:浮云皓月 一.SPAN简介 SPAN技术主要是用来监控交换机上的数据流,大体分为两种类型,本地SPAN和远程SPAN. --Lo ...
- PS快捷键