当前位置:网站首页>力扣(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
边栏推荐
- Gbase 8s Group by 功能介绍
- Qingyan environment and Shenzhen Stock Exchange listing: annual revenue of 180 million and market value of 4.1 billion
- Introduction to granularity locking of gbase 8s concurrency control
- 学习方法与职业发展指南(2022年版)
- Glide set fillet image (support custom fillet position)
- Solve the problem when installing MySQL
- [Blue Bridge Cup] [10th real topic in 2019] priority of takeout shop
- 天梯赛L2-6 树的遍历
- “自虐神器”一夜爆火:用手柄控制自己的脸,代码自取,后果自负
- 安装mysql出问题求解决
猜你喜欢

Practice and exploration of knowledge map visualization technology in meituan

The most understandable life cycle of dependency injection

Chapter 9 of C language programming (fifth edition of Tan Haoqiang) analysis and answer of exercises for users to establish their own data types

科技云报道:云计算进入“下半场”,国产云的出路在哪儿?

gin -get请求的小示例1-Handle处理GET请求

Qingyan environment and Shenzhen Stock Exchange listing: annual revenue of 180 million and market value of 4.1 billion

gin -get请求的小示例2-Handle处理post请求

“自虐神器”一夜爆火:用手柄控制自己的脸,代码自取,后果自负

Introduction to Alibaba's super large-scale Flink cluster operation and maintenance system

Futr3d: a unified 3D detection framework for sensor fusion
随机推荐
GBASE 8s 并发控制之封锁操作
Prince saves Princess (DFS)
VSCODE + PHP Debug + 名字空间指引
Gbase 8s 并发控制之粒度锁介绍
引爆炸弹(DFS)
计蒜客家谱(dfs求直系后代数)
Learning of gin framework -- golang
World reading day: 18 it books with Douban score above 9.0 are worth collecting
学习方法与职业发展指南(2022年版)
Summary of commonly used commands of LSF
leetcode771. Gemstones and stones
gin框架的学习--golang
NR polar code 七- SCL(succesive cancellation list decoding)
Gbase 8s group by function introduction
[registration] tf54: engineer growth map and excellent R & D organization building
ai2022新功能,illustrator 2022 新功能介绍
Use Charles to grab app package
d盘分给C盘后,数据库恢复挂起怎么办,求各位解答
计蒜客:方程的解数
Detonate the bomb (DFS)