当前位置:网站首页>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));
}
}
边栏推荐
- Multi branch structure
- Debezium series: modify the source code to support drop foreign key if exists FK
- 图嵌入Graph embedding学习笔记
- Flume series: interceptor filtering data
- selenium 元素信息
- After 95, Alibaba P7 published the payroll: it's really fragrant to make up this
- Autumn byte interviewer asked you any questions? In fact, you have stepped on thunder
- 【FAQ】华为帐号服务报错 907135701的常见原因总结和解决方法
- 股票开户哪里好?网上客户经理开户安全吗
- XaaS 陷阱:万物皆服务(可能)并不是IT真正需要的东西
猜你喜欢
Using repositoryprovider to simplify the value passing of parent-child components
Postman核心功能解析-参数化和测试报告
redis集群模拟消息队列
【无标题】
深度学习 卷积神经网络(CNN)基础
Inventory of the most complete low code / no code platforms in the whole network: Jiandao cloud, partner cloud, Mingdao cloud, Qingliu, xurong cloud, Jijian cloud, treelab, nailing · Yida, Tencent clo
Bitcoinwin (BCW)受邀参加Hanoi Traders Fair 2022
No matter how busy you are, you can't forget safety
软件测试是干什么的?学习有啥要求?
Force buckle 1200 Minimum absolute difference
随机推荐
Bitcoinwin (BCW)受邀参加Hanoi Traders Fair 2022
函数的概念及语法
Based on vs2017 and cmake GUI configuration, zxing and opencv are used in win10 x64 environment, and simple detection of data matrix code is realized
IBM has laid off 40 + year-old employees in a large area. Mastering these ten search skills will improve your work efficiency ten times
id选择器和类选择器的区别
【无标题】
Thread pool parameters and reasonable settings
Worthy of being a boss, byte Daniel spent eight months on another masterpiece
Flume series: interceptor filtering data
Relationship between floating elements and parent and brother boxes
C#应用程序界面开发基础——窗体控制(6)——菜单栏、工具栏和状态栏控件
Which securities company is better and which platform is safer for mobile account opening
Gstreamer中的task
SecureRandom那些事|真伪随机数
Two pits exported using easyexcel template (map empty data columns are disordered and nested objects are not supported)
Postman核心功能解析-参数化和测试报告
Win10 x64环境下基于VS2017和cmake-gui配置使用zxing以及opencv,并实现data metrix码的简单检测
Microwave radar induction module technology, real-time intelligent detection of human existence, static micro motion and static perception
95后阿里P7晒出工资单:狠补了这个,真香...
Webuploader file upload drag upload progress monitoring type control upload result monitoring control