路径总和
2025-4-20
| 2025-4-20
Words 390Read Time 1 min
type
status
date
slug
summary
tags
category
icon
password

题目:

给你二叉树的根节点 root 和一个表示目标和的整数 targetSum 。判断该树中是否存在 根节点到叶子节点 的路径,这条路径上所有节点值相加等于目标和 targetSum 。如果存在,返回 true ;否则,返回 false
叶子节点 是指没有子节点的节点。
来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/SNJvJP 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
示例 1:
notion image
pathsum1
示例 2:
notion image
pathsum2
示例 3:
提示:
  • 树中节点的数目在范围 [0, 5000]
  • 1000 <= Node.val <= 1000
  • 1000 <= targetSum <= 1000

思路:

  • dfs一层一层的遍历

代码:

结果:

notion image
image-20220416194517656
转换成小写字母跨域请求
Loading...