当前位置:网站首页>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));
}
}
边栏推荐
- 股票开户哪里好?网上客户经理开户安全吗
- What is the core value of testing?
- selenium 元素信息
- What do software test engineers do? How about the prospect of treatment?
- How to retrieve the root password of MySQL if you forget it
- Debezium series: record the messages parsed by debezium and the solutions after the MariaDB database deletes multiple temporary tables
- 图嵌入Graph embedding学习笔记
- Common operators and operator priority
- Successful entry into Baidu, 35K monthly salary, 2022 Android development interview answer
- Flume series: interceptor filtering data
猜你喜欢
Recommended collection, my Tencent Android interview experience sharing
Build your own website (16)
Interviewer: what is the internal implementation of set data types in redis?
redis集群模拟消息队列
Zhongang Mining: analysis of the current market supply situation of the global fluorite industry in 2022
40000 word Wenshuo operator new & operator delete
What is the core value of testing?
Successful entry into Baidu, 35K monthly salary, 2022 Android development interview answer
解决php无法将string转换为json的办法
S7-200smart uses V90 Modbus communication control library to control the specific methods and steps of V90 servo
随机推荐
What does software testing do? What are the requirements for learning?
What are general items
深度学习 卷积神经网络(CNN)基础
安卓面试宝典,2022Android面试笔试总结
Debezium series: record the messages parsed by debezium and the solutions after the MariaDB database deletes multiple temporary tables
Analysis of openh264 decoded data flow
SecureRandom那些事|真伪随机数
解决Thinkphp框架应用目录下数据库配置信息修改后依然按默认方式连接
Cocos2d-x项目总结中的一些遇到的问题
Go language | 03 array, pointer, slice usage
Process file and directory names
1: Citation;
selenium 元素信息
Concept and syntax of function
Win10 x64环境下基于VS2017和cmake-gui配置使用zxing以及opencv,并实现data metrix码的简单检测
Successful entry into Baidu, 35K monthly salary, 2022 Android development interview answer
Do you know several assertion methods commonly used by JMeter?
-v parameter of GST launch
IBM has laid off 40 + year-old employees in a large area. Mastering these ten search skills will improve your work efficiency ten times
Base du réseau neuronal de convolution d'apprentissage profond (CNN)