当前位置:网站首页>【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语言版

边栏推荐
猜你喜欢

二维数组的查找

Worthington解离酶:中性蛋白酶(分散酶)详情解析

Interviews with big factories under the trend of layoffs: "ByteDance"

Linux-安装MySQL(详细教程)

Introduction to Worthington Elastase & Hyaluronidase

记笔记!电源自动测试系统测试电源纹波详细方法

9 common mistakes testers fall into

【微服务~Nacos】Nacos之配置中心

微信开发者工具设置制表符大小为2

MATLAB被禁下一个会是LABVIEW吗?国产测试软件ATECLOUD崛起发力
随机推荐
二叉排序树(C语言)
软考 --- 数据库(5)数据库控制
X64 mfc140u.dll文件缺失->应用程序无法正常启动(0xc000007b)解决方法
npm ERR! code ENOTSUPnpm ERR! notsup Unsupported engine for [email protected]: wanted: {“n
高德地图jsapi不生效 INVALID_USER_SCODE
Low dropout linear regulator MPQ2013A-AEC1 brand MPS domestic replacement
Worthington细胞分离技术丨基本原代细胞分离方法和材料
Since the media increase play a short video?From the three aspects
[MySQL series] MySQL database foundation
Navicat error: 1045-Access denied for user [email protected](using passwordYES)
He used to cells harvested trypsin & release procedure
i.MX6U-driver development-3-new character driver
3 tips for using hot events to create press releases?A must-see for self-media people
canvas 中如何实现物体的框选(六)
【微服务~Nacos】Nacos之配置中心
Navicat for mysql破解版安装
Since the media how to write a short video title?Three hot style title, let your video gain more traffic
Worthington木瓜蛋白酶&胰凝乳蛋白酶&脱氧核糖核酸酶 I
记笔记!电源自动测试系统测试电源纹波详细方法
Internship in a group