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 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

题目大意

  给定二叉树,求路径之和满足目标值的路径数量。不要求从根节点开始,也不要求到叶节点为止。即路径可以为任意一段路径,但必须从上往下。

理  解:

  根据题意,在每一层它满足条件的情况都与上一层都关。对于第一层,和目标值作比较即可。第二层,需要和目标值作比较或者与目标值-第一层差值作比较。

  后面的层一次类推,直接和目标值比较或者与目标值-上一层余值的差值比较。因为每一层都可能是路径起始节点。

  使用vector保存上一层余值和目标值,若当前层值等于余值或目标值,则找到一个路径。同时,需要减去当前层的值更新vector中的值以便下一层计算。

代 码 C++:

/**
 * 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 countPath(TreeNode* root, int sum,vector<int> vec){
        if(root==NULL) return 0;
        int count = 0;
        vec.push_back(sum);
        for(int i=0;i<vec.size();++i){
            if(vec[i]==root->val)
                count++;
            vec[i] -= root->val;
        }
        count += countPath(root->left,sum,vec) + countPath(root->right,sum,vec);
        return count;
    }

    int pathSum(TreeNode* root, int sum) {
        if(root==NULL) return 0;
        int count = 0;
        vector<int> vec;
        count += countPath(root,sum,vec);
        return count;
    }
};

运行结果:

  执行用时 :72 ms, 在所有 C++ 提交中击败了8.21%的用户

  内存消耗 :63.2 MB, 在所有 C++ 提交中击败了5.29%的用户

原文地址:https://www.cnblogs.com/lpomeloz/p/11089879.html

时间: 2024-10-07 07:04:52

letecode [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

[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

LeetCode &quot;437. Path Sum III&quot;

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