当前位置:网站首页>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));
}
}

边栏推荐
- Base du réseau neuronal de convolution d'apprentissage profond (CNN)
- c語言oj得pe,ACM入門之OJ~
- What is PyC file
- 什么是pyc文件
- Reptile exercises (II)
- Add data to excel small and medium-sized cases through poi
- 再忙不能忘安全
- JVMRandom不可设置种子|问题追溯|源码追溯
- C langue OJ obtenir PE, ACM démarrer OJ
- 40000 word Wenshuo operator new & operator delete
猜你喜欢

解决Thinkphp框架应用目录下数据库配置信息修改后依然按默认方式连接

深度學習 卷積神經網絡(CNN)基礎

webuploader文件上传 拖拽上传 进度监听 类型控制 上传结果监听控件

third-party dynamic library (libcudnn.so) that Paddle depends on is not configured correctl

Let's talk about threadlocalinsecurerandom

third-party dynamic library (libcudnn.so) that Paddle depends on is not configured correctl

使用 RepositoryProvider简化父子组件的传值
![[C language] string function and Simulation Implementation strlen & strcpy & strcat & StrCmp](/img/32/738df44b6005fd84b4a9037464e61e.jpg)
[C language] string function and Simulation Implementation strlen & strcpy & strcat & StrCmp

Add data to excel small and medium-sized cases through poi

leetcode刷题:二叉树15(找树左下角的值)
随机推荐
SecureRandom那些事|真伪随机数
通过POI追加数据到excel中小案例
图嵌入Graph embedding学习笔记
Debezium series: PostgreSQL loads the correct last submission LSN from the offset
Complete interview questions for interviewers and senior Android engineers in front-line Internet enterprises
C#应用程序界面开发基础——窗体控制(5)——分组类控件
Multi branch structure
Elk distributed log analysis system deployment (Huawei cloud)
浅浅的谈一下ThreadLocalInsecureRandom
Float. The specific meaning of the return value of floattorawintbits is to convert float into byte array
Debezium series: parsing the default value character set
[C language] merge sort
Debezium series: record the messages parsed by debezium and the solutions after the MariaDB database deletes multiple temporary tables
third-party dynamic library (libcudnn.so) that Paddle depends on is not configured correctl
使用 RepositoryProvider简化父子组件的传值
IBM has laid off 40 + year-old employees in a large area. Mastering these ten search skills will improve your work efficiency ten times
期货如何网上开户?安不安全?
随机数生成的四种方法|Random|Math|ThreadLocalRandom|SecurityRandom
解决Thinkphp框架应用目录下数据库配置信息修改后依然按默认方式连接
leetcode刷题:二叉树11(平衡二叉树)