[LC] 437. Path Sum III

You are given a binary tree in which each node contains an integer value.

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:

1.  5 -> 3
2.  5 -> 2 -> 1
3. -3 -> 11

Solution 1: Time: O(N^2)Space: O(Height)
 1 # Definition for a binary tree node.
 2 # class TreeNode:
 3 #     def __init__(self, x):
 4 #         self.val = x
 5 #         self.left = None
 6 #         self.right = None
 7
 8 class Solution:
 9     def pathSum(self, root: TreeNode, sum: int) -> int:
10         if root is None:
11             return 0
12         cur = self.helper(root, sum)
13         left = self.pathSum(root.left, sum)
14         right = self.pathSum(root.right, sum)
15         return cur + left + right
16
17     def helper(self, root, sum):
18         if root is None:
19             return 0
20         left = self.helper(root.left, sum - root.val)
21         right = self.helper(root.right, sum - root.val)
22         if sum == root.val:
23             return 1 + left + right
24         return left + right

Solution 2:

Time: O(N)

Space: O(N)

 1 # Definition for a binary tree node.
 2 # class TreeNode:
 3 #     def __init__(self, x):
 4 #         self.val = x
 5 #         self.left = None
 6 #         self.right = None
 7
 8 class Solution:
 9     def pathSum(self, root: TreeNode, sum: int) -> int:
10         if root is None:
11             return 0
12         self.res = 0
13         # initilized with 0 as prefix instead of empty b/c need to conver path coming from root
14         my_dict = {0 : 1}
15         self.helper(root, sum, 0, my_dict)
16         return self.res
17
18     def helper(self, root, sum, cur_sum, my_dict):
19         if root is None:
20             return
21         cur_sum += root.val
22         reminder = cur_sum - sum
23         # use reminder as key
24         if reminder in my_dict:
25             self.res += my_dict[reminder]
26         my_dict[cur_sum] = my_dict.get(cur_sum, 0) + 1
27         # pass cur_sum to children
28         left = self.helper(root.left, sum, cur_sum, my_dict)
29         right = self.helper(root.right, sum, cur_sum, my_dict)
30         my_dict[cur_sum] -= 1

原文地址:https://www.cnblogs.com/xuanlu/p/11706617.html

时间: 2024-09-30 06:05:47

[LC] 437. Path Sum III的相关文章

437. Path Sum III

You are given a binary tree in which each node contains an integer value. 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

【easy】437. Path Sum III 二叉树任意起始区间和

/** * 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 == NU

437. Path Sum III - Easy

You are given a binary tree in which each node contains an integer value. 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

letecode [437] - Path Sum III

You are given a binary tree in which each node contains an integer value. 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

LeetCode "437. Path Sum III"

The punch line of this one: sum of leaves: pi..pj = root..pj - root..pi. /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class

LeetCode 437. Path Sum III(统计路径和等于sum的路径数量)

题意:统计路径和等于sum的路径数量. (1)节点值可正可负 (2)路径两端不一定是根结点或叶子结点 (3)路径一定是向下 分析:路径起点 (1)位于root(统计以root开头的和等于sum的路径数量) (2)位于root->left子树(递归) (3)位于root->right子树(递归) /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode

[Leetcode] Binary tree-- 437. Path Sum III

You are given a binary tree in which each node contains an integer value. 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

[LeetCode] 437. Path Sum III_ Easy tag: DFS

You are given a binary tree in which each node contains an integer value. 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

437. 二叉树路径的和 Path Sum III

You are given a binary tree in which each node contains an integer value. 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