当前位置:网站首页>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
边栏推荐
- NPM reports an error: operation not allowed, MKDIR 'C: \ program files \ node JS \ node_ cache _ cacache’
- Share the office and improve the settled experience
- How to read excel table to database
- 基于点云凸包的凹包获取方法
- Production practice elk
- L2-024 部落 (25 分)(并查集)
- Colorui solves the problem of blocking content in bottom navigation
- 搞不懂时间、时间戳、时区,快来看这篇
- Distributed message oriented middleware framework selection - Digital Architecture Design (7)
- 653. 两数之和 IV - 输入 BST
猜你喜欢

Latex paper typesetting operation

valgrind和kcachegrind使用运行分析

L2-022 rearrange linked list (25 points) (map + structure simulation)

机器学习(六)——贝叶斯分类器

PCTP考试经验分享

PLC的点表(寄存器地址和点表定义)破解探测方案--方便工业互联网数据采集

Enterprise wechat application authorization / silent login

node安装

Applet error: should have URL attribute when using navigateto, redirectto or switchtab

PLC point table (register address and point table definition) cracking detection scheme -- convenient for industrial Internet data acquisition
随机推荐
The K neighbors of each sample are obtained by packet switching
员工试用期转正申请书(泸州老窖)
MySQL small exercise (only suitable for beginners, non beginners are not allowed to enter)
Mini - exercice MySQL (seulement pour les débutants, pas pour les non - débutants)
tsdf +mvs
Little girl walking
Redis Desktop Manager for Mac
Open services in the bottom bar of idea
Find the sum of simple types of matrices
valgrind和kcachegrind使用运行分析
L2-022 rearrange linked list (25 points) (map + structure simulation)
Withholding agent
Cross domain configuration error: when allowcredentials is true, allowedorigins cannot contain the special value "*“
Latex mathematical formula
【SQL server速成之路】数据库的视图和游标
Matlab draw five-star red flag
How to protect open source projects from supply chain attacks - Security Design (1)
[Luke V0] verification environment 2 - Verification Environment components
Please arrange star trek in advance to break through the new playing method of chain tour, and the market heat continues to rise
LeetCode_DFS_中等_1254. 统计封闭岛屿的数目