Leetcode#112 Path Sum
2015-02-02 12:05
190 查看
原题地址
注意空树即使sum=0也不算
代码:
注意空树即使sum=0也不算
代码:
bool solve(TreeNode *root, int sum) { if (!root->left && !root->right) return sum == root->val; return (root->left && solve(root->left, sum - root->val)) || (root->right && solve(root->right, sum - root->val)); } bool hasPathSum(TreeNode *root, int sum) { if (!root) return false; return solve(root, sum); }
相关文章推荐
- LeetCode#112 Path Sum
- Leetcode# 112 Path Sum
- leetcode 112 —— Path Sum
- Leetcoede 112 Path Sum 二叉树
- DFS:112. Path Sum
- LeetCode 112 Path Sum
- 112题——Path Sum(二叉树,深度优先搜索,用了递归和迭代的方法)
- LeetCode 112 Path Sum
- 【leetcode c++】 112 Path Sum
- leetcode112 Path Sum
- Leetcode-112 Path Sum
- [LeetCode]112 Path Sum
- Java for LeetCode 112 Path Sum
- [leedcode 112] Path Sum
- [LeetCode]题解(python):112-Path Sum
- 112. Path Sum
- [112]. Path Sum,[113]. Path Sum II
- 112-Path Sum
- LeetCode 112 Path Sum
- Lettcode_112_Path Sum