当前位置:网站首页>112. Path sum
112. Path sum
2022-04-23 09:09:00 【yitahutu79】
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
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */
bool hasPathSum(struct TreeNode* root, int targetSum){
if (root == NULL) return false;
if (root->left == NULL && root->right == NULL) return root->val == targetSum;
return hasPathSum(root->left, targetSum - root->val) || hasPathSum(root->right, targetSum - root->val);
}
版权声明
本文为[yitahutu79]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230904164737.html
边栏推荐
- Brief steps to build a website / application using flash and H5
- Failed to download esp32 program, prompting timeout
- Strength comparison vulnerability of PHP based on hash algorithm
- 112. 路径总和
- 小女孩行走
- ALV树(LL LR RL RR)插入删除
- Employee probation application (Luzhou Laojiao)
- Notes d'apprentissage oneflow: de functor à opexprinterpreter
- ONEFLOW learning notes: from functor to opexprinter
- Distributed message oriented middleware framework selection - Digital Architecture Design (7)
猜你喜欢
Flink SQL realizes the integration of stream and batch
SAP 101K 411k inventory change
npm ERR! network
【SQL server速成之路】数据库的视图和游标
Redis Desktop Manager for Mac
[C language] document operation
L2-3 romantic silhouette (25 points)
Emuelec compilation summary
小程序报错:Cannot read property 'currentTarget' of undefined
501. 二叉搜索树中的众数
随机推荐
【原创】使用System.Text.Json对Json字符串进行格式化
653. 两数之和 IV - 输入 BST
Please arrange star trek in advance to break through the new playing method of chain tour, and the market heat continues to rise
Redis Desktop Manager for Mac
企业微信应用授权/静默登录
On array replication
错题汇总1
Download and install bashdb
Introduction to GUI programming swing
错误: 找不到或无法加载主类
MySQL小練習(僅適合初學者,非初學者勿進)
MySQL小练习(仅适合初学者,非初学者勿进)
小程序报错:Cannot read property 'currentTarget' of undefined
Trc20 fund collection solution based on thinkphp5 version
Leetcode-199 - right view of binary tree
Introduction to matlab
Flink SQL realizes the integration of stream and batch
共享办公室,提升入驻体验
Thread scheduling (priority)
NPM installation yarn