当前位置:网站首页>Yyds dry inventory solution sword finger offer: a path with a certain value in the binary tree (3)
Yyds dry inventory solution sword finger offer: a path with a certain value in the binary tree (3)
2022-06-27 16:01:00 【51CTO】
1. sketch :
describe
Given a binary tree root And an integer value sum , Find how many paths the tree has, and the sum of the node values is equal to sum .1. The problem path definition does not need to start from the root node , It doesn't need to end at the leaf node , But it must be from the father node down to the child node 2. The total number of nodes is n3. Ensure that the number of last returned paths is within the shaping range ( That is, the number of paths is less than 231-1)
Data range :
Suppose a binary tree root by {1,2,3,4,5,4,3,#,#,-1},sum=6, So the total is as follows , Yes 3 The paths meet the requirements 
Example 1
Input :
Return value :
explain :
Example 2
Input :
Return value :
Example 3
Input :
Return value :
2. Code implementation :
import java.util.*;
public class Solution {
private int res = 0;
//dfs Query the number of paths with a node as the root
private void dfs(TreeNode root, int sum){
if(root == null)
return;
// Meet the target value
if(sum == root.val)
res++;
// Enter the child node and continue to find
dfs(root.left, sum - root.val);
dfs(root.right, sum - root.val);
}
//dfs Take each node as the root query path
public int FindPath (TreeNode root, int sum) {
// If it is blank, return
if(root == null)
return res;
// Query the number of paths with a node as the root
dfs(root, sum);
// Take its child node as the new root
FindPath(root.left, sum);
FindPath(root.right, sum);
return res;
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
边栏推荐
- CentOS8-postgresql初始化时报错:initdb: error: invalid locale settings; check LANG and LC_* environment
- 洛谷入门1【顺序结构】题单题解
- 可变参数模板 Variadic Templates
- FPGA based analog I ² C protocol system design (with main code)
- 利用Redis实现订单30分钟自动取消
- 一场分销裂变活动,不止是发发朋友圈这么简单!
- VS编译遇到的问题
- 手机号码的格式
- Design principles and ideas: design principles
- Relation and operation of ORM table
猜你喜欢
![洛谷_P1002 [NOIP2002 普及组] 过河卒_dp](/img/80/4edf21e0ac316fe3dd727159621acb.png)
洛谷_P1002 [NOIP2002 普及组] 过河卒_dp

SQL parsing practice of Pisa proxy
![Beginner level Luogu 2 [branch structure] problem list solution](/img/53/d7bf659f7e1047db4676c9a01fcb42.png)
Beginner level Luogu 2 [branch structure] problem list solution

SIGKDD22|图“预训练、提示、微调”范式下的图神经网络泛化框架

郎酒两大王牌产品成都联动共振,持续带动光瓶酒消费浪潮

Li Chuang EDA learning notes 16: array copy and array distribution
![Beginner level Luogu 1 [sequence structure] problem list solution](/img/60/5e151ba31eb00374c73be52e3bfa7e.png)
Beginner level Luogu 1 [sequence structure] problem list solution
![[pygame Games] ce jeu](/img/3c/e573106ec91441a554cba18d5b2253.png)
[pygame Games] ce jeu "eat Everything" est fantastique? Tu manges tout? (avec code source gratuit)
![洛谷_P1008 [NOIP1998 普及组] 三连击_枚举](/img/9f/64b0b83211bd1c615f2db9273bb905.png)
洛谷_P1008 [NOIP1998 普及组] 三连击_枚举

A distribution fission activity is more than just a circle of friends!
随机推荐
特殊函数计算器
PSS: you are only two convolution layers away from the NMS free+ point | 2021 paper
【Pygame小游戏】这款“吃掉一切”游戏简直奇葩了?通通都吃掉嘛?(附源码免费领)
如果想用dms来处理数据库权限问题,想问下账号只能用阿里云的ram账号吗(阿里云的rds)
Does polardb-x open source support mysql5.7?
Design of CAN bus controller based on FPGA (with main codes)
Introduce you to ldbc SNB, a powerful tool for database performance and scenario testing
洛谷入门2【分支结构】题单题解
SIGKDD22|图“预训练、提示、微调”范式下的图神经网络泛化框架
Introduce you to ldbc SNB, a powerful tool for database performance and scenario testing
Markdown syntax
NFT dual currency pledge liquidity mining DAPP contract customization
Distributed session solution
字节跳动埋点数据流建设与治理实践
Regular matching starts with what, ends with what, starts with what, and ends with what
What is the open source compatibility of the current version of polardb-x? mysql8?
【170】PostgreSQL 10字段类型从字符串修改成整型,报错column cannot be cast automatically to type integer
跨域图像的衡量新方式Style relevance:论文解读和代码实战
Cesium 使用MediaStreamRecorder 或者MediaRecorder录屏并下载视频,以及开启摄像头录像。【转】
洛谷_P1003 [NOIP2011 提高组] 铺地毯_暴力枚举