当前位置:网站首页>Leetcode 112 Total path (2022.04.22)
Leetcode 112 Total path (2022.04.22)
2022-04-23 01:42:00 【ChaoYue_ miku】
Give you the root node of the binary tree root And an integer representing the sum of goals targetSum . Determine if there is Root node to leaf node The path of , The sum of the values of all nodes in this path is equal to the target and targetSum . If there is , return true ; otherwise , return false .
Leaf node A node without children .
Example 1:

Input :root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22
Output :true
explain : The root node to leaf node path equal to the target sum is shown in the figure above .
Example 2:

Input :root = [1,2,3], targetSum = 5
Output :false
explain : There are two paths from root node to leaf node in the tree :
(1 --> 2): And for 3
(1 --> 3): And for 4
non-existent sum = 5 The path from the root node to the leaf node .
Example 3:
Input :root = [], targetSum = 0
Output :false
explain : Because the tree is empty , Therefore, there is no path from root node to leaf node .
Tips :
The number of nodes in the tree is in the range [0, 5000] Inside
-1000 <= Node.val <= 1000
-1000 <= targetSum <= 1000
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/path-sum
Method 1 : recursive
C++ Submission :
/** * 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://yzsam.com/2022/04/202204230138313118.html
边栏推荐
猜你喜欢

Custom numeric input control
![[registration] tf54: engineer growth map and excellent R & D organization building](/img/c6/ae2c6427bd6af764ac614d59be54db.png)
[registration] tf54: engineer growth map and excellent R & D organization building

彻底卸载Antidote 10 ?Antidote卸载不干净怎么办?

GBase 8s 备份介绍

紫光国微财报一枝独秀 2021年净利润三位数增长靠什么

Detonate the bomb (DFS)

代码实现发邮件---sendemails

FL studio20.8最新中文版本安装下载图文教程

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

2022 low voltage electrician examination questions and answers
随机推荐
Unity结合iTextSharp生成PDF 准备dll
2022 crane driver (limited to bridge crane) examination question bank and online simulation examination
Chapter 6 uses Matplotlib to draw thermodynamic diagram
DFS奇偶性剪枝
第六章 使用 matplotlib 绘制热力图
W801 / w800 WiFi socket development (I) - UDP
GBASE 8s并发控制之封锁类型
Self taught programming, don't read theory books foolishly, programmer: it's all left over by others
Counting garlic guest: the solution of the equation
mb_substr()、mb_strpos()函数(故事篇)
Rotation slice
Redis实现分布式锁
npm——配置淘宝镜像
计蒜客:方程的解数
JSP基础知识总结
Blocking granularity of gbase 8s concurrency control
LSF的常用使用命令总结
Garlic customer: equilateral triangle (DFS)
科技云报道:云计算进入“下半场”,国产云的出路在哪儿?
iTextSharp 基础结构