[leetcode] Path Sum
来源:互联网 发布:淘宝数码宝贝大师 编辑:程序博客网 时间:2024/05/29 17:36
解法一:
/** * 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 hasPathSum(TreeNode *root, int sum) { if(root==NULL) return false; if(root->left==NULL&&root->right==NULL) return sum==root->val; return hasPathSum(root->left,sum-root->val)||hasPathSum(root->right,sum-root->val); }};
解法二:
/** * 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 hasPathSum(TreeNode *root,int sum,int curSum){ if(root==NULL) return false; curSum+=root->val; if(root->left==NULL&&root->right==NULL) return sum==curSum; return hasPathSum(root->left,sum,curSum)||hasPathSum(root->right,sum,curSum); } bool hasPathSum(TreeNode *root, int sum) { return hasPathSum(root,sum,0); }};
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
- There are no resources that can be added or removed from the server
- HDU 1754 I Hate It (线段树)
- 修改Eclipse快捷键的方法
- 7.17_java学习_构造函数_构造代码块
- java + selenium测试框架 (之高亮显示页面元素)
- [leetcode] Path Sum
- SSH的各种异常
- N个鸡蛋放进M个篮子问题
- 非法链接和重定向
- Kettle远程节点运行内存溢出
- 【华为oj1950】句子逆序
- 杭电 2019 数列有序!
- nike air max pas cher prefix22137
- POJ 3159 Candies(差分约束)