当前位置:网站首页>Twenty five of offer - all paths with a certain value in the binary tree
Twenty five of offer - all paths with a certain value in the binary tree
2022-06-26 20:55:00 【Snail Internet】
Title Description
- Enter a binary tree and an integer , Print out the sum of node values in the binary tree as all paths of input integers .
- Path is defined as a path formed from the root node of the tree to the node that the leaf node passes through .
analysis
- The first sequence traversal , Put the traversed node into A Collection
- When the leaf node is traversed and the sum is exactly the target value , Put all nodes traversed into B Collection ,B Is a path to meet the meaning of the topic
- If the sum of leaf nodes is still not equal to the target value , Then remove it A Nodes added to the collection , Modifications and , Switch to the right child node and recalculate
- If you do not traverse to the leaf node, continue to look for such a path from the child node
Binary tree definition
class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
Code implementation
public ArrayList<ArrayList<Integer>> findPath(TreeNode root,int target) {
/* * currentSum Record current and * pathNodes Save the nodes scanned by the current path * pathList Save the paths that meet the conditions */
int currentSum = 0;
ArrayList<Integer> pathNodes = new ArrayList<Integer>();
ArrayList<ArrayList<Integer>> pathList = new ArrayList<ArrayList<Integer>>();
if(root == null){
return pathList;
}
return findPath(pathList,pathNodes,root,target,currentSum);
}
private ArrayList<ArrayList<Integer>> findPath(
ArrayList<ArrayList<Integer>> pathList,
ArrayList<Integer> pathNodes,
TreeNode root,
int target,
int currentSum) {
currentSum += root.val;
pathNodes.add(root.val);
boolean isLeafNode = root.left == null && root.right == null;
// If it is a leaf node and equal to the target value , Add the current path to pathList in
if(currentSum == target && isLeafNode){
ArrayList<Integer> nodes = new ArrayList<Integer>();
for(Integer node : pathNodes){
nodes.add(node);
}
pathList.add(nodes);
}
// If it is not a leaf node, it will traverse its child nodes
if(root.left != null){
findPath(pathList, pathNodes, root.left, target, currentSum);
}
if(root.right != null){
findPath(pathList, pathNodes, root.right, target, currentSum);
}
// Before returning to the parent node , Delete the current node on the path
Integer node = pathNodes.remove(pathNodes.size() - 1);
currentSum -= node;
return pathList;
}
边栏推荐
- Development of NFT for digital collection platform
- mongoDB的三种基础备份方法
- Muke 11. User authentication and authorization of microservices
- StringUtils判断字符串是否为空
- Serial port application program based on gd32
- Gamefi active users, transaction volume, financing amount and new projects continue to decline. Can axie and stepn get rid of the death spiral? Where is the chain tour?
- C: Reverse linked list
- 0基础学c语言(2)
- Review of watermelon book (VII): Bayesian classifier (manual push + code demo)
- The postgraduate entrance examination in these areas is crazy! Which area has the largest number of candidates?
猜你喜欢

超分之VRT

GameFi 活跃用户、交易量、融资额、新项目持续性下滑,Axie、StepN 能摆脱死亡螺旋吗?链游路在何方?

Introduction to single chip microcomputer one-on-one learning strategy, independent development program immediately after reading

Development of NFT for digital collection platform

Some cold knowledge about QT database development

windows系统下怎么安装mysql8.0数据库?(图文教程)

Establish a connection with MySQL

Leetcode question brushing: String 05 (Sword finger offer 58 - ii. left rotation string)

vue中缓存组件keep-alive

Idea error: process terminated
随机推荐
[serialization] how to master the core technology of opengauss database? Secret 5: master database security (6)
C# 练习。类列表加记录,显示记录和清空记录
定长内存池
515. 在每个树行中找最大值
【贝叶斯分类3】半朴素贝叶斯分类器
Serial port application program based on gd32
What are the specific steps for opening a stock account? Is it safe to open an account online?
Sentinelresource annotation details
[most detailed] latest and complete redis interview (42 tracks)
大家都能看得懂的源码(一)ahooks 整体架构篇
Leetcode: String 04 (reverse the words in the string)
案例描述:比赛分数管理系统,需要统计历届冠军所得比赛得分,并记录到文件中,其中系统有如下需求:- 打开系统有欢迎界面,并显示可选择的选项- 选项1:记录比赛得分- 选项2:查看往届
The relationship between the development of cloud computing technology and chip processor
分布式ID生成系统
回首望月
Swagger: how to generate beautiful static document description pages
SentinelResource注解详解
【连载】说透运维监控系统01-监控系统概述
StringUtils判断字符串是否为空
Muke 11. User authentication and authorization of microservices