当前位置:网站首页>leetcode刷题:二叉树14(左叶子之和)
leetcode刷题:二叉树14(左叶子之和)
2022-07-05 19:52:00 【涛涛英语学不进去】
404.左叶子之和
计算给定二叉树的所有左叶子之和。
示例:
递归遍历,如果是 左结点 且是叶子结点 ,就把值加在一起。
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. 左叶子之和 **/
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) {
//是否为叶子结点
return true;
}
boolean leftLeaf = traversal(root.left, true);
//是否是左叶子结点
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));
}
}
边栏推荐
- 《乔布斯传》英文原著重点词汇笔记(十二)【 chapter ten & eleven】
- 完爆面试官,一线互联网企业高级Android工程师面试题大全
- Tasks in GStreamer
- What is the core value of testing?
- XaaS 陷阱:万物皆服务(可能)并不是IT真正需要的东西
- 深度學習 卷積神經網絡(CNN)基礎
- 爬虫练习题(二)
- How about testing outsourcing companies?
- 全网最全的低代码/无代码平台盘点:简道云、伙伴云、明道云、轻流、速融云、集简云、Treelab、钉钉·宜搭、腾讯云·微搭、智能云·爱速搭、百数云
- Debezium series: PostgreSQL loads the correct last submission LSN from the offset
猜你喜欢
Zhongang Mining: analysis of the current market supply situation of the global fluorite industry in 2022
【合集- 行业解决方案】如何搭建高性能的数据加速与数据编排平台
Let's talk about threadlocalinsecurerandom
acm入门day1
S7-200smart uses V90 Modbus communication control library to control the specific methods and steps of V90 servo
CADD课程学习(7)-- 模拟靶点和小分子相互作用 (半柔性对接 AutoDock)
S7-200SMART利用V90 MODBUS通信控制库控制V90伺服的具体方法和步骤
Win10 x64环境下基于VS2017和cmake-gui配置使用zxing以及opencv,并实现data metrix码的简单检测
[Collection - industry solutions] how to build a high-performance data acceleration and data editing platform
淺淺的談一下ThreadLocalInsecureRandom
随机推荐
力扣 1200. 最小绝对差
Gstreamer中的task
不愧是大佬,字节大牛耗时八个月又一力作
函数的概念及语法
C - sequential structure
openh264解码数据流向分析
JVMRandom不可设置种子|问题追溯|源码追溯
国信证券在网上开户安全吗?
Redis cluster simulated message queue
Interviewer: what is the internal implementation of set data types in redis?
手机股票开户安全吗?靠不靠谱啊?
深度學習 卷積神經網絡(CNN)基礎
建议收藏,我的腾讯Android面试经历分享
Debezium series: modify the source code to support UNIX_ timestamp() as DEFAULT value
Debezium series: parsing the default value character set
Win10 x64环境下基于VS2017和cmake-gui配置使用zxing以及opencv,并实现data metrix码的简单检测
UWB ultra wideband positioning technology, real-time centimeter level high-precision positioning application, ultra wideband transmission technology
太牛了,看这篇足矣了
What do software test engineers do? How about the prospect of treatment?
[OBS] qstring's UTF-8 Chinese conversion to blog printing UTF-8 char*