当前位置:网站首页>LeetCode_二叉树_前缀和_中等_437. 路径总和 III
LeetCode_二叉树_前缀和_中等_437. 路径总和 III
2022-06-09 09:10:00 【一瓢江湖我沉浮】
1.题目
给定一个二叉树的根节点 root ,和一个整数 targetSum ,求该二叉树里节点值之和等于 targetSum 的路径的数目。
路径不需要从根节点开始,也不需要在叶子节点结束,但是路径方向必须是向下的(只能从父节点到子节点)。
示例 1:
输入:root = [10,5,-3,3,2,null,11,3,-2,null,1], targetSum = 8
输出:3
解释:和等于 8 的路径有 3 条,如图所示。
示例 2:
输入:root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
输出:3
提示:
二叉树的节点个数的范围是 [0,1000]
-109 <= Node.val <= 109
-1000 <= targetSum <= 1000
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/path-sum-iii
2.思路
(1)前缀和
有关前缀和的题目,可参考LeetCode_前缀和_中等_560.和为 K 的子数组这题。本题需要将前缀和的知识运用到二叉树中。
3.代码实现(Java)
//思路1————前缀和
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */
class Solution {
/* 记录前缀和 从二叉树的根节点开始,路劲和为 pathSum 的路径有 preSumCnt.get(pathSum) 条 */
HashMap<Integer, Integer> preSumCnt = new HashMap<>();
int pathSum, targetSum;
int res = 0;
public int pathSum(TreeNode root, int targetSum) {
if (root == null) {
return 0;
}
this.pathSum = 0;
this.targetSum = targetSum;
this.preSumCnt.put(0, 1);
traverse(root);
return res;
}
public void traverse(TreeNode root) {
if (root == null) {
return;
}
//前序遍历位置
pathSum += root.val;
/* 从二叉树的根节点开始,路径和为 pathSum - targetSum 的路径条数 就是路径和为 targetSum 的路径条数 */
res += preSumCnt.getOrDefault(pathSum - targetSum, 0);
//记录从二叉树的根节点开始,路径和为 pathSum 的路径条数
preSumCnt.put(pathSum, preSumCnt.getOrDefault(pathSum, 0) + 1);
traverse(root.left);
traverse(root.right);
//后序遍历位置
preSumCnt.put(pathSum, preSumCnt.get(pathSum) - 1);
pathSum -= root.val;
}
}
边栏推荐
猜你喜欢

. Net C # Foundation (6): namespace - a sharp tool for organizing code

MySQL basic knowledge

MySQL basic multi table query

three.js学习笔记(十六)——汹涌的海洋

Kusionstack has a sense of open source | it took two years to break the dilemma of "separating lines like mountains"

neo4j实现社交推荐(五)
![[texstudio] [3] relatively complete paper typesetting template and bib file reference method](/img/ba/160b236aaf0fc905609b8a70c7677d.jpg)
[texstudio] [3] relatively complete paper typesetting template and bib file reference method

Sofa weekly | kusion open source, QA this week, contributor this week

Draw multiple polygons using canvas

Summary of Android development interview experience and compilation of actual records (must see)
随机推荐
C语言指针
Have fun | sofaark source code analysis activity
Qt development -- compilation of serial port assistant
MySQL基础 子查询
2022-2028 global UAV detection and jamming system industry survey and trend analysis report
MySQL基础 基础认知
Neo4j realizes social recommendation (V)
Document sorting (expansion)
neo4j实现社交推荐(五)
. Net C # Foundation (6): namespace - a sharp tool for organizing code
数据库问题MySQL
Codeworks round 797 (Div. 3) a~e problem solving record
Learn about graph database neo4j (I)
【Redis学习12】分布式缓存之哨兵机制,分片集群
MySQL basic DML and DDL learning
Countdown 3 days 𞓜 sofachannel 28 sofaark class isolation framework design
MySQL基础 增删改查练习
Leetcode game 295
Omit application reduces TS duplicate codes
MySQL basic database creation foundation