当前位置:网站首页>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
边栏推荐
- Unity combines itextsharp to generate PDF preparation DLL
- 力扣(LeetCode)112. 路径总和(2022.04.22)
- 腾讯云接口进行人脸检测 和签名出错问题
- The most easy to understand service container and scope of dependency injection
- 什么是布尔类型?
- Gbase 8s 并发控制之封锁粒度
- LSF的常用使用命令总结
- leetcode771. Gemstones and stones
- Problem solving: dpkg DEB: error: package name has characters that are't lowercase alphanums or '- +‘
- Counting garlic customers: Sudoku (DFS)
猜你喜欢

42、使用mmrotate中k3det进行旋转目标检测,并进行mnn部署和ncnn部署

2022 melting welding and thermal cutting operation certificate examination question simulation examination platform operation

Counting garlic guest: the solution of the equation

DFS奇偶性剪枝

GBase 8s 备份介绍

2022 low voltage electrician examination questions and answers

腾讯云接口进行人脸检测 和签名出错问题

Completely uninstall antidote 10? What if the antidote uninstall is not clean?

Summary of LSF usage

科技云报道:云计算进入“下半场”,国产云的出路在哪儿?
随机推荐
K zeros after leetcode factorial function
角色個人屬性英文縮寫
2022 low voltage electrician examination questions and answers
Gbase 8s数据库日志简介及管理
Introduction to granularity locking of gbase 8s concurrency control
iTextSharp 基础结构
Gbase 8s 并发控制之封锁粒度
New functions of ai2022, introduction to new functions of illustrator 2022
轮转法分片
Gbase 8s存儲結構簡介及空間管理
npm——配置淘宝镜像
GBase8s SQL 引擎框架简介
Gbase 8s存储结构简介及空间管理
Jisuan Hakka spectrum (DFS for direct post algebra)
什么时候应该编写单元测试?什么是TDD?
Oracle database query lock table SQL script and delete lock information script (necessary for database development ETL and DBA)
Redis implements distributed locks
安装mysql出问题求解决
Counting garlic customers: Sudoku (DFS)
Tight coupling of visual wheel odometer