LeetCode : Path Sum III
来源:互联网 发布:下载12360软件下载 编辑:程序博客网 时间:2024/05/21 17:39
Find the number of paths that sum to a given value.
The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only from parent nodes to child nodes).
The tree has no more than 1,000 nodes and the values are in the range -1,000,000 to 1,000,000.
Example:
root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8
10 / \5 -3
/ \ \
3 2 11
/ \ \
3 -2 1
Return 3. The paths that sum to 8 are:
- 5 -> 3
- 5 -> 2 -> 1
- -3 -> 11
/** * 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: int pathSum(TreeNode* root, int sum) { if(root==NULL) return 0; return dfs(root,sum)+pathSum(root->left,sum)+pathSum(root->right,sum); } int dfs(TreeNode *root,int sum) { int res = 0; if(root==NULL) return res; if(sum==root->val) res++; res+=dfs(root->left,sum-root->val); res+=dfs(root->right,sum-root->val); return res; }};
0 0
- LeetCode-Path Sum III
- path sum iii-leetcode
- Path Sum III ---LeetCode
- Leetcode | Path Sum III
- Leetcode Path Sum III
- LEETCODE--Path Sum III
- LeetCode -- Path Sum III
- LeetCode : Path Sum III
- LeetCode 437. Path Sum III
- LeetCode 437. Path Sum III
- LeetCode 437. Path Sum III
- leetcode 437. Path Sum III
- [LeetCode]437. Path Sum III
- LeetCode 437 Path Sum III
- 【LeetCode】 437. Path Sum III
- [leetcode]437. Path Sum III
- LeetCode 437. Path Sum III
- [leetcode]437. Path Sum III
- EasyUI tab加载iframe高度不适应
- Summernote – 基于 Bootstrap 的文本编辑器
- 紫书动规 例题9-7 UVA
- iOS最笨的办法实现无限轮播图(网络加载)
- C#多态与虚方法
- LeetCode : Path Sum III
- EF 与 sql server 转到mysql
- Node.js的安装和配置
- 射频识别技术漫谈(20)——RC系列射频接口芯片
- 手机端调用相机上传图片
- 第十、Java的二维数组
- PTA 阶乘升级版
- 实现基于内存的HPA
- ACM习题O-15