LeetCode-Path Sum
来源:互联网 发布:js转换dom 编辑:程序博客网 时间:2024/06/06 09:55
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.
Code:
<span style="font-size:14px;">/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: bool helper(TreeNode *root, const int &sum, int tempSum) { if (!root) return false; tempSum += root->val; if (tempSum == sum && !root->left && !root->right) return true; if (!root->left && !root->right) return false; if (helper(root->left, sum, tempSum)) return true; if (helper(root->right, sum, tempSum)) return true; return false; } bool hasPathSum(TreeNode *root, int sum) { return helper(root, sum, 0); }};</span>
0 0
- leetcode Path Sum && Path Sum ||
- 【Leetcode】Path Sum (Sum)
- leetcode:leetcode :Path Sum
- 【Leetcode】Path Sum II (Sum)
- LeetCode: Minimum Path Sum
- LeetCode: Path Sum
- LeetCode: Path Sum II
- LeetCode Minimum Path Sum
- LeetCode Path Sum
- LeetCode Path Sum II
- [Leetcode] Path Sum
- [Leetcode] Path Sum II
- LeetCode : Minimum Path Sum
- [Leetcode] Minimum Path Sum
- LeetCode: Path Sum
- LeetCode: Path Sum II
- [LeetCode] Path Sum
- [LeetCode] Path Sum II
- poj1637 Sightseeing tour,混合图的欧拉回路问题,最大流解
- Struts1与Struts2的区别
- Tomcat 集群问题
- 小车PID算法跑直线
- UVA 586 - Instant Complexity(dfs)
- LeetCode-Path Sum
- PV操作摘抄题 待续
- [Phonegap+Sencha Touch] 移动开发16 安卓webview中,input输入框不触发backspace回退键事件的解决办法(带来其他bug,作废)
- [].length = 0
- OpenCV 之Hello world篇
- 【翻译自mos文章】oracle数据库的最大数据容量限制和表空间的最大数据容量限制
- 100+ 100A root教程_方法
- Centos 6.5 Nginx1.6.0 + php5.4.31 + mysql 环境搭建成功。
- poj 3404 Bridge over a rough river(过桥问题)