当前位置:网站首页>力扣(LeetCode)112. 路径总和(2022.04.22)
力扣(LeetCode)112. 路径总和(2022.04.22)
2022-04-23 01:38:00 【ChaoYue_miku】
给你二叉树的根节点 root 和一个表示目标和的整数 targetSum 。判断该树中是否存在 根节点到叶子节点 的路径,这条路径上所有节点值相加等于目标和 targetSum 。如果存在,返回 true ;否则,返回 false 。
叶子节点 是指没有子节点的节点。
示例 1:
输入:root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22
输出:true
解释:等于目标和的根节点到叶节点路径如上图所示。
示例 2:
输入:root = [1,2,3], targetSum = 5
输出:false
解释:树中存在两条根节点到叶子节点的路径:
(1 --> 2): 和为 3
(1 --> 3): 和为 4
不存在 sum = 5 的根节点到叶子节点的路径。
示例 3:
输入:root = [], targetSum = 0
输出:false
解释:由于树是空的,所以不存在根节点到叶子节点的路径。
提示:
树中节点的数目在范围 [0, 5000] 内
-1000 <= Node.val <= 1000
-1000 <= targetSum <= 1000
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/path-sum
方法一:递归
C++提交内容:
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */
class Solution {
public:
bool hasPathSum(TreeNode *root, int sum) {
if (root == nullptr) {
return false;
}
if (root->left == nullptr && root->right == nullptr) {
return sum == root->val;
}
return hasPathSum(root->left, sum - root->val) ||
hasPathSum(root->right, sum - root->val);
}
};
版权声明
本文为[ChaoYue_miku]所创,转载请带上原文链接,感谢
https://chaoyue.blog.csdn.net/article/details/124357557
边栏推荐
- engine. Post() handles post requests
- Project manager's thinking mode worth trying: project success equation
- JSP基础知识总结
- 计蒜客:数独(DFS)
- W801/W800/W806唯一ID/CPUID/FLASHID
- Counting garlic customers: Sudoku (DFS)
- Full Permutation (DFS and next_permutation solution)
- Deployment of mask detection flash for yolov5
- DFS parity pruning
- LSF的常用使用命令总结
猜你喜欢
W801/W800/W806唯一ID/CPUID/FLASHID
彻底卸载Antidote 10 ?Antidote卸载不干净怎么办?
"Self abuse artifact" exploded overnight: control your face with a handle, take your own code, and bear the consequences
Level 4 city area table xlsx, SQL file, domestic, provincial, county, street, township level 4 address in China (name, linkage ID, level, end level or not (1-yes))
NR polar code 七- SCL(succesive cancellation list decoding)
2022 penetration job interview (thinking)
代码实现发邮件---sendemails
Abused "architect"!
Detonate the bomb (DFS)
Innovative practice of short video content understanding and generation technology in meituan
随机推荐
Gbase 8t index
Summary of commonly used commands of LSF
“自虐神器”一夜爆火:用手柄控制自己的脸,代码自取,后果自负
Jerry's AI server [chapter]
2022 melting welding and thermal cutting operation certificate examination question simulation examination platform operation
天梯赛L2-6 树的遍历
找数字(DFS)
无关联进程间通信 —— 命名管道的创建和使用
engine.POST()处理POST请求
Slow response of analysis API
Counting garlic guest: the solution of the equation
d盘分给C盘后,数据库恢复挂起怎么办,求各位解答
engine. Post() handles post requests
Android sqliteopenhelper data table structure upgrade
Abused "architect"!
Solve the problem when installing MySQL
Unrelated interprocess communication -- creation and use of named pipes
[Blue Bridge Cup] [10th real topic in 2019] priority of takeout shop
计蒜客(踏青)(染色块问题的DFS和BFS解法)
Gbase 8s query processing and optimization