112. Path Sum
来源:互联网 发布:笔记本无线蹭网软件 编辑:程序博客网 时间:2024/06/07 23:00
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
For example:Given the below binary tree and
sum = 22
,5 / \ 4 8 / / \ 11 13 4 / \ \ 7 2 1
return true, as there exist a root-to-leaf path 5->4->11->2
which sum is 22.
依然是迭代
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution
{
public:
bool hasPathSum(TreeNode* root, int sum)
{
if(root==NULL)
return false;
else if(root->left==NULL&&root->right==NULL&&root->val==sum)
return true;
else
return hasPathSum(root->left,sum-root->val)||hasPathSum(root->right,sum-root->val);
}
};
- 112. Path Sum && 113. Path Sum II
- 112. Path Sum&113. Path Sum II
- 112.Path Sum
- LeetCode 112. Path Sum
- [LeetCode]112.Path Sum
- 【LeetCode】112.Path Sum
- [Leetcode] 112. Path Sum
- [leetcode] 112.Path Sum
- 112.Path Sum
- 112. Path Sum
- 112. Path Sum
- 112. Path Sum
- 112. Path Sum
- 112. Path Sum LeetCode
- 112. Path Sum
- 112. Path Sum
- 112. Path Sum
- 112. Path Sum
- 让input type=text 文本输入框的文字居中显示
- 消息队列五
- spirngmvc js传递复杂json参数到controller
- 小程序开发之获取token值
- Linux下安装JDK
- 112. Path Sum
- 利用OpenCV查找圆[HoughCircles]
- HTTP重定向负载均衡
- css网页大于屏幕分辨率,打开网页让网页居中显示
- python-day03-File
- 35 个 Java 代码性能优化总结
- 润乾报表v5通过权限控制数据访问方案
- 仪器仪表信息化系统对企业的三大好处
- NOIP模拟9.27