leetcode 112. Path Sum
来源:互联网 发布:美工连捅策划7刀新闻 编辑:程序博客网 时间:2024/06/17 01:31
题目:
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.
代码:
class Solution {public: bool hasPathSum(TreeNode* root, int sum) { if(root == NULL) return false; if(root->left == NULL && root->right == NULL && root->val == sum) return true; return hasPathSum(root->left, sum-root->val) || hasPathSum(root->right, sum-root->val); }};
0 0
- LeetCode 112. Path Sum
- [LeetCode]112.Path Sum
- 【LeetCode】112.Path Sum
- [Leetcode] 112. Path Sum
- [leetcode] 112.Path Sum
- 112. Path Sum LeetCode
- Leetcode 112. Path Sum
- LeetCode *** 112. Path Sum
- leetcode-112. Path Sum
- Leetcode 112. Path Sum
- 112.[Leetcode]Path Sum
- LeetCode 112. Path Sum
- Leetcode 112. Path Sum
- 【leetcode】112. Path Sum
- Leetcode 112. Path Sum
- LeetCode - 112. Path Sum
- [Leetcode]112. Path Sum
- LeetCode 112. Path Sum
- 2017.5.14
- 0514
- Spark2.1.0分布式集群安装
- activiti标准restful例子
- 已经添加了jar包却还是出现 java.lang.ClassNotFoundException: com.mysql.jdbc.Driver错误
- leetcode 112. Path Sum
- AngularJS中$state带参数跳转
- WWW BROWSER AND SERVER
- 消息队列
- Zookeeper系列(二十六)Zookeeper之Ganglia监控安装
- 软件研发软件基础设施的建设
- 笔记&讨论:什么是模块化开发以及最基本的应用
- 2017江苏省赛赛后感想
- 安卓中实现动态显示时间(基础+运用),自定义控件