当前位置:网站首页>二叉树——112. 路径总和
二叉树——112. 路径总和
2022-07-25 23:55:00 【向着百万年薪努力的小赵】
1 题目描述
给你二叉树的根节点 root 和一个表示目标和的整数 targetSum 。判断该树中是否存在 根节点到叶子节点 的路径,这条路径上所有节点值相加等于目标和 targetSum 。如果存在,返回 true ;否则,返回 false 。
叶子节点 是指没有子节点的节点。
题目描述来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/path-sum
2 题目示例


示例 3:
输入:root = [], targetSum = 0
输出:false
解释:由于树是空的,所以不存在根节点到叶子节点的路径。
3 题目提示
树中节点的数目在范围 [0, 5000] 内
-1000 <= Node.val <= 1000
-1000 <= targetSum <= 1000
4 思路
注意到本题的要求是,询问是否有从「根节点」到某个「叶子节点」经过的路径上的节点之和等于目标和。核心思想是对树进行一次遍历,在遍历时记录从根节点到当前节点的路径和,以防止重复计算。
需要特别注意的是,给定的 root 可能为空。
方法一:广度优先搜索
首先我们可以想到使用广度优先搜索的方式,记录从根节点到当前节点的路径和,以防止重复计算。
这样我们使用两个队列,分别存储将要遍历的节点,以及根节点到这些节点的路径和即可。
复杂度分析
- 时间复杂度:o(N),其中N是树的节点数。对每个节点访问一次。
- 空间复杂度:o(N),其中N是树的节点数。空间复杂度主要取决于队列的开销,队列中的元素个数不会超过树的节点数。
方法二:递归
观察要求我们完成的函数,我们可以归纳出它的功能:询问是否存在从当前节点root到叶子节点的路径,满足其路径和为sum。
假定从根节点到当前节点的值之和为val ,我们可以将这个大问题转化为一个小问题:是否存在从当前节点的子节点到叶子的路径,满足其路径和为sum - va1 。
不难发现这满足递归的性质,若当前节点就是叶子节点,那么我们直接判断sum是否等于va1即可(因为路径和已经确定,就是当前节点的值,我们只需要判断该路径和是否满足条件)。若当前节点不是叶子节点,我们只需要递归地询问它的子节点是否能满足条件即可。
复杂度分析
- 时间复杂度:O(N),其中N是树的节点数。对每个节点访问一次。
- 空间复杂度:O(H),其中H是树的高度。空间复杂度主要取决于递归时栈空间的开销,最坏情况下,树呈现链状,空间复杂度为o(N)。平均情况下树的高度与节点数的对数正相关,空间复杂度为O(log N)。
5 我的答案
方法一:广度优先搜索
class Solution {
public boolean hasPathSum(TreeNode root, int sum) {
if (root == null) {
return false;
}
Queue<TreeNode> queNode = new LinkedList<TreeNode>();
Queue<Integer> queVal = new LinkedList<Integer>();
queNode.offer(root);
queVal.offer(root.val);
while (!queNode.isEmpty()) {
TreeNode now = queNode.poll();
int temp = queVal.poll();
if (now.left == null && now.right == null) {
if (temp == sum) {
return true;
}
continue;
}
if (now.left != null) {
queNode.offer(now.left);
queVal.offer(now.left.val + temp);
}
if (now.right != null) {
queNode.offer(now.right);
queVal.offer(now.right.val + temp);
}
}
return false;
}
}
方法二:递归
class Solution {
public boolean hasPathSum(TreeNode root, int sum) {
if (root == null) {
return false;
}
if (root.left == null && root.right == null) {
return sum == root.val;
}
return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
}
}
边栏推荐
- Call nailing API and report an error: the signature sent by the robot is expired; Solution: please keep the signature generation time and sending time within timestampms
- typescript ts 基础知识之类
- Part 67: conversion between keypoint and point2f in opencv
- Using jetpack libraries in applications
- 下一代终端安全管理的关键特征与应用趋势
- TOPSIS and entropy weight method
- initializer_ List tool library learning
- 二叉树——617. 合并二叉树
- SIGIR‘22 推荐系统论文之图网络篇
- Part 66: monocular 3D reconstruction point cloud
猜你喜欢

什么是奇偶校验?如何用C语言实现?

initializer_ List tool library learning

Article 75: writing skills of academic papers

Leetcode 0135. distribute candy

C language implementation of three chess

ABAP 代码中读取会计科目的字段状态(隐藏、可选、必输)

C# - readonly 和 const 关键字

ArcGIS cuts TIF images (grid data) according to the vector range, merges shp files in batches, cuts vectors in the region according to the vector range, outputs the geographic coordinate system, conve

Topsis与熵权法

红娘的话
随机推荐
Exercise (1) create a set C1 to store the elements "one", "two", "three"“
[learning notes] unreal 4 engine introduction (IV)
[Muduo] thread package
抽丝剥茧C语言(高阶)程序环境和预处理
Part 74: overview of machine learning optimization methods and superparameter settings
From which dimensions can we judge the quality of code? How to have the ability to write high-quality code?
[Muduo] package EventLoop and thread
统计之歌 歌词
Leetcode 0919. complete binary tree inserter: array representation of complete binary tree
Getting started with Servlet
Unexpected dubug tricks
老旧笔记本电脑变服务器(笔记本电脑+内网穿透)
二叉树——530.二叉搜索树的最小绝对差
[learning notes] unreal 4 engine introduction (III)
JS synchronization and asynchrony
1223. Dice simulation range DP
寻找链表的中间节点
【英雄哥七月集训】第 24天: 线性树
Nacos offline service times error errcode: 500
initializer_ List tool library learning