当前位置:网站首页>Leetcode brush question: binary tree 14 (sum of left leaves)
Leetcode brush question: binary tree 14 (sum of left leaves)
2022-07-05 19:59:00 【Taotao can't learn English】
404. Sum of left leaves
Calculate the sum of all the left leaves of a given binary tree .
Example :
Recursive traversal , If it is Left node And it is a leaf node , Just add up the values .
package com.programmercarl.tree;
import com.programmercarl.util.GenerateTreeNode;
/** * @ClassName SumOfLeftLeaves * @Descriotion TODO * @Author nitaotao * @Date 2022/7/5 10:54 * @Version 1.0 * https://leetcode.cn/problems/sum-of-left-leaves/ * 404. Sum of left leaves **/
public class SumOfLeftLeaves {
Integer sum = 0;
public int sumOfLeftLeaves(TreeNode root) {
traversal(root, false);
return sum;
}
public boolean traversal(TreeNode root, boolean isLeft) {
if (root == null) {
// Is it a leaf node
return true;
}
boolean leftLeaf = traversal(root.left, true);
// Is it a left leaf node
if (leftLeaf && isLeft && root.left == null && root.right == null) {
sum += root.val;
}
traversal(root.right, false);
return false;
}
public static void main(String[] args) {
TreeNode root = GenerateTreeNode.generateTreeNode("[0,2,4,1,null,3,-1,5,1,null,6,null,8]");
System.out.println(new SumOfLeftLeaves().sumOfLeftLeaves(root));
}
}
边栏推荐
- Debezium series: parsing the default value character set
- 通配符选择器
- 【硬核干货】数据分析哪家强?选Pandas还是选SQL
- Jvmrandom cannot set seeds | problem tracing | source code tracing
- Build your own website (16)
- 618 "low key" curtain call, how can baiqiushangmei join hands with the brand to cross the "uncertain era"?
- 解决php无法将string转换为json的办法
- Concept and syntax of function
- Float.floatToRawIntBits的返回值具体意思,将float转为byte数组
- C application interface development foundation - form control (6) - menu bar, toolbar and status bar controls
猜你喜欢
[FAQ] summary of common causes and solutions of Huawei account service error 907135701
Autumn byte interviewer asked you any questions? In fact, you have stepped on thunder
Go language | 03 array, pointer, slice usage
How about testing outsourcing companies?
IBM has laid off 40 + year-old employees in a large area. Mastering these ten search skills will improve your work efficiency ten times
Common - Hero Minesweeper
[OBS] qstring's UTF-8 Chinese conversion to blog printing UTF-8 char*
S7-200smart uses V90 Modbus communication control library to control the specific methods and steps of V90 servo
redis集群模拟消息队列
安卓面试宝典,2022Android面试笔试总结
随机推荐
[C language] string function and Simulation Implementation strlen & strcpy & strcat & StrCmp
After 95, Alibaba P7 published the payroll: it's really fragrant to make up this
[C language] merge sort
Is it safe for Guosen Securities to open an account online?
sun. misc. Base64encoder error reporting solution [easy to understand]
2023年深圳市绿色低碳产业扶持计划申报指南
【c语言】归并排序
js实现禁止网页缩放(Ctrl+鼠标、+、-缩放有效亲测)
字节跳动Dev Better技术沙龙成功举办,携手华泰分享Web研发效能提升经验
Float.floatToRawIntBits的返回值具体意思,将float转为byte数组
Android interview classic, 2022 Android interview written examination summary
C application interface development foundation - form control (6) - menu bar, toolbar and status bar controls
C application interface development foundation - form control (5) - grouping control
再忙不能忘安全
通过POI追加数据到excel中小案例
Complete interview questions for interviewers and senior Android engineers in front-line Internet enterprises
Relationship between floating elements and parent and brother boxes
建立自己的网站(16)
That's awesome. It's enough to read this article
How to apply smart contracts more wisely in 2022?