当前位置:网站首页>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
边栏推荐
- [C language] document operation
- LeetCode396. Rotate array
- OpenCV中的图像处理 —— 轮廓入门+轮廓特征
- Complete binary search tree (30 points)
- 政务中台研究目的建设目标,建设意义,技术创新点,技术效果
- Taxable income
- Idea package jar file
- [Luke V0] verification environment 2 - Verification Environment components
- Multi view depth estimation by fusing single view depth probability with multi view geometry
- ALV树(LL LR RL RR)插入删除
猜你喜欢

Brief steps to build a website / application using flash and H5

Program, process, thread; Memory structure diagram; Thread creation and startup; Common methods of thread

Consensus Token:web3. 0 super entrance of ecological flow

L2-024 tribe (25 points) (and check the collection)

Distributed message oriented middleware framework selection - Digital Architecture Design (7)

ONEFLOW learning notes: from functor to opexprinter

Pctp test experience sharing

MySQL小练习(仅适合初学者,非初学者勿进)

Redis Desktop Manager for Mac

108. 将有序数组转换为二叉搜索树
随机推荐
Concave hull acquisition method based on convex hull of point cloud
First principle mind map
Resource packaging dependency tree
JS prototype chain
小女孩行走
LeetCode_DFS_中等_1254. 统计封闭岛屿的数目
Share the office and improve the settled experience
数字政府建设中政务中台中的技术创新点
错题汇总1
Brief steps to build a website / application using flash and H5
论文阅读《Multi-View Depth Estimation by Fusing Single-View Depth Probability with Multi-View Geometry》
L2-3 romantic silhouette (25 points)
MySQL小練習(僅適合初學者,非初學者勿進)
How to protect open source projects from supply chain attacks - Security Design (1)
L2-022 重排链表 (25 分)(map+结构体模拟)
Failed to download esp32 program, prompting timeout
[SQL Server fast track] view and cursor of database
Cross domain configuration error: when allowcredentials is true, allowedorigins cannot contain the special value "*“
Write down the post order traversal of the ~ binary tree
GoLand debug go use - white record