Sum of Left Leaves
https://www.lintcode.com/problem/sum-of-left-leaves/description
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
private int sum = 0;
/**
* @param root: t
* @return: the sum of all left leaves
*/
public int sumOfLeftLeaves(TreeNode root) {
// Write your code here
tree(root, false);
return sum;
}
private void tree(TreeNode root, boolean b) {
if (root == null) {
return;
}
TreeNode left = root.left;
TreeNode right = root.right;
if (left == null && right == null) {
if (b) {
sum += root.val;
}
}
tree(left, true);
tree(right, false);
}
}
相關文章
- 404-Sum of Left Leaves
- LeetCode之Sum of Left Leaves(Kotlin)LeetCodeKotlin
- LeetCode C++ 1302. Deepest Leaves Sum【Tree/BFS/DFS】中等LeetCodeC++
- PAT Advanced 1004 Counting Leaves
- Solution - Codeforces 622E Ants in Leaves
- mysql + left joinMySql
- SQL Server Left joinSQLServer
- SQL Server LEFT FunctionsSQLServerFunction
- Kaggle樹葉分類Leaves Classify總結
- 秒殺 2Sum 3Sum 4Sum 演算法題演算法
- GCD SUMGC
- Sum Problem
- 集合sum
- 15+18、3Sum 4Sum
- 【MySQL】LEFT JOIN 踩坑MySql
- SQL Union 和left join
- Range Minimum Sum
- B - Minimum Sum
- Missing Subsequence Sum
- Path Sum III
- Leetcode Path SumLeetCode
- leetcode Sum系列LeetCode
- Path-sum
- mysql left join轉inner joinMySql
- oracle update left join查詢Oracle
- LeetCode | 1 Two SumLeetCode
- SQL groupby sum 用法SQL
- 7.22 APPROX_SUMAPP
- the Sum of Cube hd 5053
- Leetcode 39 Combination SumLeetCode
- md5sum
- 112-Path Sum
- Leetcode 1 two sumLeetCode
- Codeforces Round #646 (Div. 2)【C. Game On Leaves 題解】GAM
- sql:left join和join區別SQL
- MySQL 之 LEFT JOIN 避坑指南MySql
- MySQL LEFT JOIN/ INNER JOIN/RIGHT JOINMySql
- LEFT JOIN 和JOIN 多表連線