当前位置:网站首页>LeetCode_ Binary tree_ Prefix and_ Medium_ 437. path sum III
LeetCode_ Binary tree_ Prefix and_ Medium_ 437. path sum III
2022-06-09 09:41:00 【I've been up and down in the Jianghu】
1. subject
Given the root node of a binary tree root , And an integer targetSum , Find that the sum of the node values in the binary tree is equal to targetSum The number of paths for .
The path doesn't need to start at the root , It doesn't need to end at the leaf node , But the path has to be down ( Only from parent node to child node ).
Example 1:
Input :root = [10,5,-3,3,2,null,11,3,-2,null,1], targetSum = 8
Output :3
explain : And equal to 8 There are 3 strip , As shown in the figure .
Example 2:
Input :root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
Output :3
Tips :
The range of the number of nodes in a binary tree is [0,1000]
-109 <= Node.val <= 109
-1000 <= targetSum <= 1000
source : Power button (LeetCode)
link :https://leetcode.cn/problems/path-sum-iii
2. Ideas
(1) The prefix and
Questions about prefixes and , May refer to LeetCode_ The prefix and _ secondary _560. And for K Subarray This question . This topic needs to apply the knowledge of prefix and to the binary tree .
3. Code implementation (Java)
// Ideas 1———— The prefix and
/** * 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 {
/* Record prefix and Start with the root node of the binary tree , Lu jinhewei pathSum There are preSumCnt.get(pathSum) strip */
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;
}
// Preorder traversal position
pathSum += root.val;
/* Start with the root node of the binary tree , Path and are pathSum - targetSum The number of paths Is the path and for targetSum The number of paths */
res += preSumCnt.getOrDefault(pathSum - targetSum, 0);
// The record starts from the root node of the binary tree , Path and are pathSum The number of paths
preSumCnt.put(pathSum, preSumCnt.getOrDefault(pathSum, 0) + 1);
traverse(root.left);
traverse(root.right);
// Postorder traversal position
preSumCnt.put(pathSum, preSumCnt.get(pathSum) - 1);
pathSum -= root.val;
}
}
边栏推荐
- MySQL基础 数据类型精讲
- Neo4j error when accessing the browser: serviceunavailable: websocket connection failure Due to security constraints in your
- . Net C # Foundation (6): namespace - a sharp tool for organizing code
- 最小路径和
- 附十七章 網絡程序解讀限定文章
- Annexe 17 interprétation du programme réseau
- SOFA Weekly | Kusion 开源啦、本周 QA、本周 Contributor
- GLCC 首届编程夏令营|欢迎报名 Layotto、KusionStack、Nydus、Kata Containers!
- MySQL basic query statement
- HAVE FUN | SOFAArk 源码解析活动
猜你喜欢

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

【图机器学习】图神经网络入门(一)谱图理论

TS 泛型的extends属性

MySQL基础 数据类型精讲

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

Wechat applet - doodle Conference - conference release and my conference view

论文理解【RL - Exp Replay】—— An Equivalence between Loss Functions and Non-Uniform Sampling in Exp Replay

Anatomy of illusory rendering system (15) - XR topic

SOFA Weekly | Kusion 开源啦、本周 QA、本周 Contributor

TS 泛型类和泛型接口的好处
随机推荐
面试官:如何秒开视频?什么是秒开视频?
Anatomy of illusory rendering system (15) - XR topic
Kusionstack has a sense of open source | it took two years to break the dilemma of "separating lines like mountains"
three.js学习笔记(十五)——着色器图案
ERP 系统,编译和学习
MySQL基础 函数篇
GLCC's first programming summer camp welcome to sign up for layotto, kusionstack, Nydus, Kata containers!
[redis learning 13] redis builds master-slave cluster, sentinel cluster and partition cluster
Detailed introduction to MySQL basic data types
Advanced web service (VIII) base64decoder
[5机器学习]全网最易懂的决策树(附源码)
Array.map()简写函数
Understand the graph database neo4j (II)
Wechat applet obtains user information and updates it
将文件流(InputStream)写入文件 将上传文件MultipartFile写到文件
GLCC 首届编程夏令营|欢迎报名 Layotto、KusionStack、Nydus、Kata Containers!
2022-2028 global linear lamp industry research and trend analysis report
【新手上路常见问答】平面设计的基本原则
Have fun | sofaark source code analysis activity
neo4j实现社交推荐(四)