LeetCode: Path Sum
2013-04-19 04:22
399 查看
少数次过
C#
View Code
/** * 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) { // Start typing your C/C++ solution below // DO NOT write int main() function if (!root) return false; if (!root->left && !root->right) return sum == root->val; return hasPathSum(root->left, sum-root->val) || hasPathSum(root->right, sum-root->val); } };
C#
/** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNode left; * public TreeNode right; * public TreeNode(int x) { val = x; } * } */ public 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); } }
View Code
相关文章推荐
- leetcode -- Path Sum
- LeetCode112. Path Sum(Easy)
- [LeetCode] Path Sum 二叉树的路径和
- leetCode 64.Minimum Path Sum (最短路) 解题思路和方法
- leetcode 64. Minimum Path Sum
- leetcode112---Path Sum
- LeetCode 64. Minimum Path Sum
- leetcode - 64.Minimum Path Sum
- leetcode No64. Minimum Path Sum
- LeetCode 64.Minimum Path Sum
- Leetcode-112 Path Sum
- leetcode 64. Minimum Path Sum
- [LeetCode] Path Sum, Solution
- [LeetCode] Path Sum
- leetcode 112: Path Sum
- LeetCode: Path Sum
- leetcode_题解_path sum_easy
- Java [Leetcode 112]Path Sum
- LeetCode 112. Path Sum
- [LeetCode] Path Sum