当前位置:网站首页>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
边栏推荐
- 领导/老师让填写电子excel表格文档可手机上如何编辑word/excel文件填写excel/word电子文档?
- Abused "architect"!
- 2022 low voltage electrician examination questions and answers
- . net unit test Part 1: common Net unit test framework?
- 客户端项目管理经常面临的挑战
- Introduction and management of gbase 8s database log
- GBASE 8s分片表管理操作
- Gbase 8s 共享内存段删除
- VSCODE + PHP Debug + 名字空间指引
- Qingyan environment and Shenzhen Stock Exchange listing: annual revenue of 180 million and market value of 4.1 billion
猜你喜欢
The most easy to understand service container and scope of dependency injection
Soatest preliminary understanding
Chris LATTNER, father of llvm: the golden age of compilers
Counting garlic guest: the solution of the equation
最长公共子序列(记录路径版)
[经验教程]支付宝余额自动转入余额宝怎么设置关闭取消支付宝余额自动转入余额宝?
腾讯云接口进行人脸检测 和签名出错问题
Abused "architect"!
[course summary] Hello harmonyos series of courses, take you to zero foundation introduction
Futr3d: a unified 3D detection framework for sensor fusion
随机推荐
最新流程引擎 flowable 6.7.2 更新说明
全排列(DFS和next_permutation解法)
Summary of LSF usage
01 knapsack problem - and deformation problem
Itextsharp page setup
Digital collection platform settled in digital collection platform to develop SaaS platform of digital collection
Solve the problem when installing MySQL
Summary of commonly used commands of LSF
Learning methods and career development guide (2022 Edition)
.NET单元测试第一篇:常见.NET单元测试框架有哪些?
W801/W800/W806唯一ID/CPUID/FLASHID
Introduction and management of gbase 8s database log
2022第六季完美童模 IPA国民赛领跑元宇宙赛道
W801 / w800 WiFi socket development (II) - UDP Bluetooth control WiFi connection
W801 / w800 / w806 unique ID / CPUID / flashid
Oracle database query lock table SQL script and delete lock information script (necessary for database development ETL and DBA)
The most understandable life cycle of dependency injection
计蒜客(踏青)(染色块问题的DFS和BFS解法)
四级城市地区表 xlsx, sql文件,国内,中国省市县街道乡镇四级地址 (名称,联动ID,层级,是否末级(1-是))
Counting garlic customers: Sudoku (DFS)