LeetCode //C - 437. Path Sum III

本文介绍了如何使用深度优先搜索(DFS)算法解决LeetCode题目437,计算给定二叉树中节点值之和等于目标和的路径数量。通过递归函数实现,从根节点开始遍历,统计满足条件的路径总数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

437. Path Sum III

Given the root of a binary tree and an integer targetSum, return the number of paths where the sum of the values along the path equals targetSum.

The path does not need to start or end at the root or a leaf, but it must go downwards (i.e., traveling only from parent nodes to child nodes).
 

Example 1:

在这里插入图片描述

Input root = [10,5,-3,3,2,null,11,3,-2,null,1], targetSum = 8
Output 3
Explanation: The paths that sum to 8 are shown.

Example 2:

Input root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
Output 3

Constraints:
  • The number of nodes in the tree is in the range [0, 1000].
  • − 1 0 9 < = N o d e . v a l < = 1 0 9 -10^9 <= Node.val <= 10^9 109<=Node.val<=109
  • -1000 <= targetSum <= 1000

From: LeetCode
Link: 437. Path Sum III


Solution:

Ideas:
  1. Struct TreeNode: This is a definition for a binary tree node which contains an integer value and pointers to the left and right child nodes.

  2. DFS Function: This recursive function takes a node and the sums targetSum and currentSum as arguments, along with a pointer to an integer count that keeps track of the number of valid paths found.

    • As it traverses the tree, it adds the value of the current node to currentSum.
    • If currentSum equals targetSum, it increments the count.
    • It then recursively calls itself for the left and right children of the current node, passing along the updated currentSum.
  3. pathSumFrom Function: This function is called for each node in the tree. It uses the dfs function to explore all paths that start from the current node.

    • It also recursively calls itself on the left and right children of the current node to ensure that paths starting from those nodes are also counted.
  4. pathSum Function: This is the main function that initiates the path sum calculation. It starts the process by calling pathSumFrom for the root of the tree.

    • It maintains a count of the number of valid paths and returns this count as the result of the function.
Code:
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */

void dfs(struct TreeNode* node, long long targetSum, long long currentSum, int* count) {
    if (!node) return;

    currentSum += node->val;
    if (currentSum == targetSum) {
        *count += 1;
    }

    dfs(node->left, targetSum, currentSum, count);
    dfs(node->right, targetSum, currentSum, count);
}

void pathSumFrom(struct TreeNode* node, long long targetSum, int* count) {
    if (!node) return;

    dfs(node, targetSum, 0, count);

    pathSumFrom(node->left, targetSum, count);
    pathSumFrom(node->right, targetSum, count);
}

int pathSum(struct TreeNode* root, int targetSum) {
    int count = 0;
    pathSumFrom(root, (long long)targetSum, &count);  // Explicitly cast targetSum to long long
    return count;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Navigator_Z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值