112. Path Sum
来源:互联网 发布:8管脚单片机 编辑:程序博客网 时间:2024/06/04 00:52
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->val == sum && root->left == NULL && root->right == NULL) return true; return hasPathSum(root->left, sum-root->val) || hasPathSum(root->right, sum-root->val); }};
dalao的代码永远那么简洁!!!尤其最后一句,碉堡!
阅读全文
0 0
- 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
- linux 多路转接之select
- Bottom Sheet的使用和BottomSheetDialogFragment的封装
- 详解java定时任务
- 关于open函数,一个很容易出错的问题
- 技术博客003
- 112. Path Sum
- 用rvest包爬取豆瓣图书数据
- Activity的launchMode介绍
- matlab 全局最优算法 GlobalSearch
- 郁闷!ionic中获取ng-model绑定的值为undefined如何解决
- java——与c++之不同——其他
- No mapping found for HTTP request with URI
- hdu5972 快速匹配bitset优化(shiftand算法)2016ACM/ICPC亚洲区大连站
- 更快的方式实现PHP数组去重(转)