当前位置:网站首页>【LeetCode每日一题】——404.左叶子之和
【LeetCode每日一题】——404.左叶子之和
2022-07-30 00:52:00 【IronmanJay】
一【题目类别】
- 二叉树
二【题目难度】
- 简单
三【题目编号】
- 404.左叶子之和
四【题目描述】
- 给定二叉树的根节点 root ,返回所有左叶子之和。
五【题目示例】
示例 1:
输入: root = [3,9,20,null,null,15,7]
输出: 24
解释: 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24示例 2:
输入: root = [1]
输出: 0
六【题目提示】
- 节点数在 [1, 1000] 范围内
- -1000 <= Node.val <= 1000
七【解题思路】
- 这个题思路比较简单,首先想到递归,但是应该怎么进行递归呢?
- 首先我们要求的节点应该是左子树节点
- 其次这个左子树节点还应该是叶子节点
- 基于以上两点进行判断,将其求和返回即可
八【时间频度】
- 时间复杂度: O ( l o g 2 N ) O(log_{2}N) O(log2N),其中 N N N为树的结点个数
- 空间复杂度: O ( l o g 2 N ) O(log_{2}N) O(log2N),其中 N N N为树的结点个数
九【代码实现】
- Java语言版
package Tree;
public class p404_SumOfLeftLeaves {
int val;
p404_SumOfLeftLeaves left;
p404_SumOfLeftLeaves right;
public p404_SumOfLeftLeaves() {
}
public p404_SumOfLeftLeaves(int val) {
this.val = val;
}
public p404_SumOfLeftLeaves(int val, p404_SumOfLeftLeaves left, p404_SumOfLeftLeaves right) {
this.val = val;
this.left = left;
this.right = right;
}
public static void main(String[] args) {
p404_SumOfLeftLeaves root = new p404_SumOfLeftLeaves(3);
p404_SumOfLeftLeaves left = new p404_SumOfLeftLeaves(9);
p404_SumOfLeftLeaves right = new p404_SumOfLeftLeaves(20);
p404_SumOfLeftLeaves right1 = new p404_SumOfLeftLeaves(15);
p404_SumOfLeftLeaves right2 = new p404_SumOfLeftLeaves(7);
root.left = left;
root.right = right;
right.left = right1;
right.right = right2;
int res = sumOfLeftLeaves(root);
System.out.println("res = " + res);
}
public static int sumOfLeftLeaves(p404_SumOfLeftLeaves root) {
if (root == null) {
return 0;
}
int res = 0;
if (root.left != null && root.left.left == null && root.left.right == null) {
res += root.left.val;
}
return res + sumOfLeftLeaves(root.left) + sumOfLeftLeaves(root.right);
}
}
- C语言版
#include<stdio.h>
struct TreeNode
{
int val;
struct TreeNode *left;
struct TreeNode *right;
};
int sumOfLeftLeaves(struct TreeNode* root)
{
if (root == NULL)
{
return 0;
}
int res = 0;
if (root->left != NULL && root->left->left == NULL && root->left->right == NULL)
{
res += root->left->val;
}
return res + sumOfLeftLeaves(root->left) + sumOfLeftLeaves(root->right);
}
/*主函数省略*/
十【提交结果】
Java语言版
C语言版
边栏推荐
- [Experience] Experience Summary - Lessons Learned
- MySql的初识感悟,以及sql语句中的DDL和DML和DQL的基本语法
- Nacos配置中心用法详细介绍
- 【Flutter】Flutter inspector 工具使用详解,查看Flutter布局,widget树,调试界面等
- Mysql internal and external connections
- LeetCode / Scala - 无重复字符最长子串 ,最长回文子串
- 更换可执行文件glibc版本的某一次挣扎
- How Filebeat ensures that the log file is still correctly read when the log file is split (or rolled)
- 【微服务~Nacos】Nacos之配置中心
- 华为“天才少年”稚晖君又出新作,从零开始造“客制化”智能键盘
猜你喜欢
Detailed explanation of nacos cluster configuration
Running a Fabric Application
Worthington Enzymatic Cell Harvest & Cell Adhesion and Harvest
Linux - install MySQL (detailed tutorial)
Navicat for mysql破解版安装
He used to cells harvested trypsin & release procedure
Navicat for mysql crack version installation
从尾到头打印链表
自学HarmonyOS应用开发(49)- 引入地图功能
Towhee 每周模型
随机推荐
裁员趋势下的大厂面试:“字节跳动”
what is a .pro file in qt
I.MX6U-驱动开发-3-新字符驱动
【励志】科比精神
高德地图jsapi不生效 INVALID_USER_SCODE
KDE Frameworks 5.20.0:Plasma迎来诸多改进
中文语义匹配
自学HarmonyOS应用开发(53)- 获取当前位置
Worthington dissociating enzyme: detailed analysis of neutral protease (dispase)
LeetCode/Scala - without the firstborn of the string of characters, the longest text string back
每周推荐短视频:研发效能是什么?它可以实现反“内卷”?
Google Chrome (google) is set to translate Chinese, the translation option does not take effect or the translation option does not pop up
what is a .pro file in qt
Worthington解离酶:胰蛋白酶及常见问题
推荐系统:特征工程、常用特征
低压差线性稳压器MPQ2013A-AEC1品牌MPS国产替代
基于SSM实现个性化健康饮食推荐系统
LeetCode / Scala - 无重复字符最长子串 ,最长回文子串
Worthington细胞分离技术丨基本原代细胞分离方法和材料
@RequestParam注解的详细介绍