LeetCode 112. Path Sum

原题链接在这里:https://leetcode.com/problems/path-sum/

题目:

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.

题解:

减掉当前节点值,判断是不是叶子节点同时满足sum==0即可。

Time Complexity: O(n), worst case是每个node都跑了一次.

Space: O(logn), stack space.

AC Java:

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode() {}
 8  *     TreeNode(int val) { this.val = val; }
 9  *     TreeNode(int val, TreeNode left, TreeNode right) {
10  *         this.val = val;
11  *         this.left = left;
12  *         this.right = right;
13  *     }
14  * }
15  */
16 class Solution {
17     public boolean hasPathSum(TreeNode root, int targetSum) {
18         if(root == null){
19             return false;
20         }
21         
22         targetSum -= root.val;
23         if(root.left == null && root.right == null && targetSum == 0){
24             return true;
25         }
26         
27         return hasPathSum(root.left, targetSum) || hasPathSum(root.right, targetSum);
28     }
29 }

AC C++:

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 8  *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 9  *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10  * };
11  */
12 class Solution {
13 public:
14     bool hasPathSum(TreeNode* root, int targetSum) {
15         if(!root){
16             return false;
17         }
18 
19         targetSum -= root->val;
20         if(!root->left && !root->right && targetSum == 0){
21             return true;
22         }
23 
24         return hasPathSum(root->left, targetSum) || hasPathSum(root->right, targetSum);
25     }
26 };

AC Python:

 1 # Definition for a binary tree node.
 2 # class TreeNode:
 3 #     def __init__(self, val=0, left=None, right=None):
 4 #         self.val = val
 5 #         self.left = left
 6 #         self.right = right
 7 class Solution:
 8     def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
 9         if not root:
10             return False
11         
12         targetSum -= root.val
13         if not root.left and not root.right and targetSum == 0:
14             return True
15         
16         return self.hasPathSum(root.left, targetSum) or self.hasPathSum(root.right, targetSum)

跟上Path Sum IIPath Sum IIIPath Sum IV.

类似Sum Root to Leaf Numbers.

posted @ 2015-11-20 14:37  Dylan_Java_NYC  阅读(277)  评论(0编辑  收藏  举报