当前位置:网站首页>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.
边栏推荐
- Slow bear market, bit Store provides stable stacking products to help you cross the bull and bear
- 分布式Session解决方案
- Eolink 推出面向中小企业及初创企业支持计划,为企业赋能!
- Leetcode daily practice (Yanghui triangle)
- ORM表关系及操作
- What should the ultimate LAN transmission experience be like
- What are the password requirements for waiting insurance 2.0? What are the legal bases?
- In the Alibaba cloud experiment, if the k8s forwards to port 3306 and the MySQL client is turned on, it will terminate abnormally. What is the reason?
- Leetcode daily practice (longest substring without repeated characters)
- E ModuleNotFoundError: No module named ‘psycopg2‘(已解决)
猜你喜欢
PSS: vous n'êtes qu'à deux niveaux du NMS Free + Lifting point | 2021 Paper
【kotlin】第二天
Mobile terminal click penetration
Bit. Store: long bear market, stable stacking products may become the main theme
Domain name binding dynamic IP best practices
A distribution fission activity is more than just a circle of friends!
鴻蒙發力!HDD杭州站·線下沙龍邀您共建生態
Eolink launched a support program for small and medium-sized enterprises and start-ups to empower enterprises!
Leetcode daily practice (longest substring without repeated characters)
28 object method extension
随机推荐
Design of spread spectrum communication system based on FPGA (with main code)
事务的隔离级别详解
Design of digital video signal processor based on FPGA (with main code)
跨域图像的衡量新方式Style relevance:论文解读和代码实战
FPGA based analog I ² C protocol system design (with main code)
Jialichuang EDA professional edition all offline client release
Numerical extension of 27es6
Difference between special invoice and ordinary invoice
Bit. Store: long bear market, stable stacking products may become the main theme
Luogu_ P1003 [noip2011 improvement group] carpet laying_ Violence enumeration
Basic configuration and usage of Jupiter notebook
NFT dual currency pledge liquidity mining DAPP contract customization
Cesium uses mediastreamrecorder or mediarecorder to record screen and download video, as well as turn on camera recording. [transfer]
基于 Nebula Graph 构建百亿关系知识图谱实践
About fast exponentiation
请问阿里云实验中 k8s 对于3306端口转发,同时开启mysql客户端就会异常终止,是什么原因呢?
利用Redis实现订单30分钟自动取消
PSS:你距離NMS-free+提點只有兩個卷積層 | 2021論文
express
28 object method extension