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.
/**
* 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) {
int[] total = new int[];
helper(root, false, total);
return total[];
} void helper(TreeNode root, boolean isLeft, int[] total) {
if (root == null) return;
if (root.left == null && root.right == null) {
if (isLeft) {
total[] += root.val;
}
return;
} helper(root.left, true, total);
helper(root.right, false, total); }
}
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
404. Sum of Left Leaves Easy Find the sum of all left leaves in a given binary tree. Example: 3 / \ ...
- LeetCode 404. 左叶子之和(Sum of Left Leaves)
404. 左叶子之和 404. Sum of Left Leaves LeetCode404. Sum of Left Leaves 题目描述 计算给定二叉树的所有左叶子之和. 示例: 3 / \ 9 ...
- [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
Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...
- LeetCode Sum of Left Leaves
原题链接在这里:https://leetcode.com/problems/sum-of-left-leaves/ 题目: Find the sum of all left leaves in a g ...
- 404. Sum of Left Leaves
Find the sum of all left leaves in a given binary tree. 左树的值(9+15=24) /** * Definition for a binary ...
- 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 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 ...
随机推荐
- LoadScript
function loadScripts(urls, callback) { if (typeof (urls) === "string"){ urls = [urls]; } v ...
- 今天讲的是JQ 的动画效果
老规矩,先贴代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...
- Junit初级编码(二)探索JUnit核心
序,Junit测试是单元测试的一个框架,提供了很多方法,供我们快速开展单元测试.现在就让我们慢慢学习Junit单元测试框架 一.Junit的三个核心概念测试类.测试集.测试运行器 1 测试类 公共的, ...
- WCF 已知类型和泛型解析程序 KnownType
数据协定继承 已知类型和泛型解析程序 Juval Lowy 下载代码示例 自首次发布以来,Windows Communication Foundation (WCF) 开发人员便必须处理数据协定继承方 ...
- 使用LIBSVM工具实现样本分类预测——MatLab
准备工作: https://www.csie.ntu.edu.tw/~cjlin/libsvm/,下载LIBSVM:(LIBSVM工具相较于MATLAB自带的工具:1).支持多分类及回归(‘-s 0’ ...
- mac jdk环境变量
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk /Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk ...
- Net上传附件大小控控值(转)
Server Error 404 – File or directory not found. The resource you are looking for might have been rem ...
- poj3070 (斐波那契,矩阵快速幂)
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9630 Accepted: 6839 Descrip ...
- Smarty缓存技术总结
大家应该都知道合理使用缓存能有效的减轻网站的服务器压力,php Smarty作为一个非常优秀的php模板引擎,它为我们提供了非常简单而多样化的缓存操作,下面就让我们学习一下smarty缓存操作方面的一 ...
- 请求WebMethod, Ajax 处理更加专注
在WebForm下 开发ajax程序,需要借助于一般处理程序(*.ashx)或web服务(*.asmx),并且每一个ajax请求,都要建一个这样的文件,如此一来,如 果在一个项目中ajax程序多了,势 ...