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 Solution {
    int ret;
    void go(TreeNode *p, int sofar, int tgt, unordered_map<int, unsigned> rec)
    {
        if(!p) return;

        sofar += p->val;
        if(sofar == tgt)
        {
            ret ++; // case 1 : from root
        }

        int t = sofar - tgt;
        if(rec.find(t) != rec.end())
        {
            ret += rec[t];
        }

        rec[sofar] ++;
        go(p->left, sofar, tgt, rec);
        go(p->right,sofar, tgt, rec);
    }
public:
    int pathSum(TreeNode* root, int sum)
    {
        ret = 0;
        if(!root) return 0;

        go(root, 0, sum, unordered_map<int, unsigned>());
        return ret;
    }
};
时间: 2024-08-06 00:33:43

LeetCode "437. Path Sum III"的相关文章

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

【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

[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】Path Sum II

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 思路:与[Leetcode]Path Sum 不同

LeetCode:Path Sum - 树的根节点到叶节点的数字之和

1.题目名称 Path Sum(树的根节点到叶节点的数字之和) 2.题目地址 https://leetcode.com/problems/path-sum/ 3.题目内容 英文: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. 中文:给定一颗二叉树,如