当前位置:网站首页>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;
}
边栏推荐
- Tiktok practice ~ search page ~ scan QR code
- 好物推薦:移動端開發安全工具
- Arduino uno + DS1302 uses 31 byte static RAM to store data and print through serial port
- Super VRT
- 大家都能看得懂的源码(一)ahooks 整体架构篇
- mysql存储过程
- 0 basic C language (2)
- The relationship between the development of cloud computing technology and chip processor
- Stringutils judge whether the string is empty
- 阿里云个人镜像仓库日常基本使用
猜你喜欢

Super VRT
![[Bayesian classification 2] naive Bayesian classifier](/img/44/dbff297e536508a7c18b76b21db90a.png)
[Bayesian classification 2] naive Bayesian classifier

Unity——Mathf. Similarities and differences between atan and atan2

基于QT实现简单的连连看小游戏

【连载】说透运维监控系统01-监控系统概述

云计算技术的发展与芯片处理器的关系

Idea error: process terminated

mongoDB的三种基础备份方法

Leetcode question brushing: String 06 (implement strstr())

12个MySQL慢查询的原因分析
随机推荐
C language file cursor fseek
C: 反转链表
分布式ID生成系统
不要做巨嬰了
Developer survey: rust/postgresql is the most popular, and PHP salary is low
Leetcode question brushing: String 06 (implement strstr())
leetcode刷题:字符串04(颠倒字符串中的单词)
[serialization] how to master the core technology of opengauss database? Secret 5: master database security (6)
Good thing recommendation: mobile terminal development security tool
Tiktok practice ~ search page ~ scan QR code
网上办理中金财富开户安全吗?
Keep alive cache component in Vue
【贝叶斯分类2】朴素贝叶斯分类器
定长内存池
leetcode刷题:字符串02( 反转字符串II)
MySQL中存储过程的详细详解
C语言 文件光标 fseek
基于QT实现简单的连连看小游戏
Disruptor local thread queue_ Use transprocessor processor and workpool to compare consumption - Notes on inter thread communication 005
The relationship between the development of cloud computing technology and chip processor