404-Sum of Left Leaves
Description
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.
問題描述
求出二叉樹的所有左葉子的值的和
問題分析
解法
class Solution {
public int sumOfLeftLeaves(TreeNode root) {
return sumOfLeftLeavesHelper(root, false);
}
private int sumOfLeftLeavesHelper(TreeNode root, boolean cameFromLeft) {
if (root == null) return 0;
if (root.left == null && root.right == null) return cameFromLeft ? root.val : 0;
return 0 + sumOfLeftLeavesHelper(root.left, true) + sumOfLeftLeavesHelper(root.right, false);
}
}
相關文章
- Sum of Left Leaves
- LeetCode之Sum of Left Leaves(Kotlin)LeetCodeKotlin
- 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總結
- 【MySQL】LEFT JOIN 踩坑MySql
- SQL Union 和left join
- mysql left join轉inner joinMySql
- oracle update left join查詢Oracle
- Codeforces Round #646 (Div. 2)【C. Game On Leaves 題解】GAM
- LeetCode C++ 1302. Deepest Leaves Sum【Tree/BFS/DFS】中等LeetCodeC++
- sql:left join和join區別SQL
- MySQL 之 LEFT JOIN 避坑指南MySql
- MySQL LEFT JOIN/ INNER JOIN/RIGHT JOINMySql
- LEFT JOIN 和JOIN 多表連線
- 513-Find Bottom Left Tree Value
- mysql left join 優化學習MySql優化
- CF976-F. Count Leaves-min25篩(紅溫了)
- 沒有磁碟空間 No space left on devicedev
- MYSQL 怎麼 LEFT JOIN 多表聯查MySql
- 《One Leaves》控煙體驗遊戲為什麼達不到效果遊戲
- openGauss 出現-Error-No-space-left-on-device-提示Errordev
- MYSQL count標量子查詢改left joinMySql
- CSS @page:left列印偽類選擇器CSS
- Sql 中的 left 函式、right 函式SQL函式
- [LeetCode] 2516. Take K of Each Character From Left and RightLeetCode
- TiDB資料庫left join與版本問題TiDB資料庫
- CSS中 offsetLeft 與style.left 的區別CSS
- mysql常用連線查詢join,left,right,crossMySqlROS
- 深入理解mysql之left join 使用詳解MySql
- 一個left join SQL 簡單優化分析SQL優化
- write /usr/include/openssl/cms.h: no space left on devicedev
- Inner Join, Left Outer Join和Association的區別
- Mysql-left join on後接and,和where的區別MySql
- java踩坑之java.io.IOException: No space left on deviceJavaExceptiondev