【LeetCode】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

answer:

  This problem is similar to the interval problem of strings or arrays, interval sum, product, etc. The most straightforward idea is to enumerate all possible intervals. This question traverses the tree first, and enumerates whether the sum of all intervals starting from the traversed node is equal to sum during the traversal process.

Solution 1

 1 class Solution {
 2 public:
 3     int pathSum(TreeNode* root, int sum) {
 4         if (!root)
 5             return 0;
 6         int res = 0;
 7         stack<TreeNode*> st;
 8         TreeNode* cur = root;
 9         while (cur || !st.empty()) {
10             if (cur) {
11                 st.push(cur);
12                 cur = cur->left;
13             } else {
14                 cur = st.top();
15                 st.pop();
16                 int num = 0;
17                 dfs(cur, num, sum);
18                 res += num;
19                 cur = cur->right;
20             }
21         }
22         return res;
23     }
24     void dfs(TreeNode* root, int& num, int sum) {
25         if (!root)
26             return;
27         if (root->val == sum) {
28             ++num;
29         }
30         dfs(root->left, num, sum - root->val);
31         dfs(root->right, num, sum - root->val);
32         
33     }
34 };

 

Solution 2

  The sum of the tree with the root as the root node = the number of satisfied conditions in the interval with the root as the starting point + the sum of the tree with the left child of the root as the root node + the sum of the tree with the root node of the right child of the root, to obtain a recursive relationship.

  Note that dfs searches for an interval with root as the starting point, while pathSum searches for a tree with root as the root node. PathSum does not require that the interval must start with root.

 1 class Solution {
 2 public:
 3     int pathSum(TreeNode* root, int sum) {
 4         if (!root)
 5             return 0;
 6         return dfs(root, sum) + pathSum(root->left, sum) + pathSum(root->right, sum);
 7     }
 8     int dfs(TreeNode* root, int sum) {
 9         int num = 0;
10         if (!root)
11             return 0;
12         if (root->val == sum) {
13             ++num;
14         }
15         num += dfs(root->left, sum - root->val);
16         num += dfs(root->right, sum - root->val);
17         return num;
18     }
19 };

 

Solution 3

  The idea uses the hash storage idea commonly used in the array string interval. Generally speaking, it is not commonly used in the tree. Strictly speaking, it should be somewhat similar to the prefix sum. Grandyang

 1 class Solution {
 2 public:
 3     int pathSum(TreeNode* root, int sum) {
 4         unordered_map<int, int> m;
 5         m[0] = 1;
 6         return helper(root, sum, 0, m);
 7     }
 8     
 9     int helper(TreeNode* node, int sum, int curSum, unordered_map<int, int>& m) {
10         if( node) 
 11              return  0 ;
12          curSum += node-> val;
13          int res = m[curSum - sum];
14          ++ m[run];
15          res += helper(node->left, sum, curSum, m) + helper(node-> right, sum, curSum, m);
16          - m[cursum];
17          return res;
18      }
 19 };

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324384609&siteId=291194637