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

边栏推荐
- Relationship between floating elements and parent and brother boxes
- 《乔布斯传》英文原著重点词汇笔记(十二)【 chapter ten & eleven】
- Go language learning tutorial (XV)
- openh264解码数据流向分析
- leetcode刷题:二叉树14(左叶子之和)
- 深度學習 卷積神經網絡(CNN)基礎
- Cocos2d-x项目总结中的一些遇到的问题
- Go language | 02 for loop and the use of common functions
- C langue OJ obtenir PE, ACM démarrer OJ
- Base du réseau neuronal de convolution d'apprentissage profond (CNN)
猜你喜欢

CADD课程学习(7)-- 模拟靶点和小分子相互作用 (半柔性对接 AutoDock)

leetcode刷题:二叉树18(最大二叉树)

Database logic processing function

Force buckle 729 My schedule I

Bitcoinwin (BCW) was invited to attend Hanoi traders fair 2022

leetcode刷题:二叉树15(找树左下角的值)

Autumn byte interviewer asked you any questions? In fact, you have stepped on thunder
Complete interview questions for interviewers and senior Android engineers in front-line Internet enterprises

How about testing outsourcing companies?

How to select the Block Editor? Impression notes verse, notation, flowus
随机推荐
深度学习 卷积神经网络(CNN)基础
Android interview classic, 2022 Android interview written examination summary
挖财钱堂教育靠谱安全吗?
Analysis of openh264 decoded data flow
JVMRandom不可设置种子|问题追溯|源码追溯
Is it safe to open a mobile stock account? Is it reliable?
Redis cluster simulated message queue
Is it safe for Anxin securities to open an account online?
股票开户哪里好?网上客户经理开户安全吗
解决php无法将string转换为json的办法
2023年深圳市绿色低碳产业扶持计划申报指南
What is the core value of testing?
众昂矿业:2022年全球萤石行业市场供给现状分析
Go language learning tutorial (16)
[untitled]
Debezium series: idea integrates lexical and grammatical analysis ANTLR, and check the DDL, DML and other statements supported by debezium
Four methods of random number generation | random | math | threadlocalrandom | securityrandom
【obs】libobs-winrt :CreateDispatcherQueueController
40000 word Wenshuo operator new & operator delete
Flume series: interceptor filtering data