当前位置:网站首页>Leetcode 113. 路径总和 II
Leetcode 113. 路径总和 II
2022-07-25 13:18:00 【LuZhouShiLi】
Leetcode 113. 路径总和 II
题目
给你二叉树的根节点 root 和一个整数目标和 targetSum ,找出所有 从根节点到叶子节点 路径总和等于给定目标和的路径。
叶子节点 是指没有子节点的节点。
思路
- 首先创建一个vector<vector> resuLt结果数组,用来保存路径path数组
- 先序遍历,首先判断当前节点的左子树和右子树是不是空,count是否为0,如果是,说明找到了一条路径,存储该路径
- 递归遍历左子树,首先将当前节点存入path中,计数器count减去当前节点值,然后递归遍历,如果返回了不成功,直接撤销上次存储的结果。
代码
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */
class Solution {
public:
vector<vector<int>> result;
vector<int> path;
void traversal(TreeNode* cur,int count)
{
// 遇到叶子节点 并且count = 0 保存路径
if(!cur->left && !cur->right && count == 0)
{
result.push_back(path);
return;
}
// 遇到叶子节点且,且没有找到合适的边,直接返回
if(!cur->left && !cur->right)
{
return;
}
if(cur->left)
{
// 递归遍历左子树
path.push_back(cur->left->val);// 存入当前节点值
count -= cur->left->val;
traversal(cur->left,count); // 递归
count += cur->left->val;// 回溯
path.pop_back();// 回溯
}
if(cur->right)
{
// 递归遍历右子树
path.push_back(cur->right->val);
count -= cur->right->val;
traversal(cur->right,count);// 递归
count += cur->right->val;// 回溯
path.pop_back();// 回溯
}
return;
}
vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
result.clear();
path.clear();
if(root == NULL)
{
return result;
}
path.push_back(root->val);// 将根节点送入路径中
traversal(root,targetSum - root->val);
return result;
}
};
边栏推荐
- R语言GLM广义线性模型:逻辑回归、泊松回归拟合小鼠临床试验数据(剂量和反应)示例和自测题
- 若依如何实现用户免密登录配置方法?
- C#基础学习(二十三)_窗体与事件
- Handwriting a blog platform ~ first day
- [CSDN year-end summary] end and start, always on the way - "2021 summary of" 1+1= Wang "
- 说说对hashcode和equals方法的理解?
- My creation anniversary
- How to solve the problem of taking up too much space when recording and editing videos?
- Shell常用脚本:获取网卡IP地址
- C # basic learning (XXIII)_ Forms and events
猜你喜欢
![[Video] Markov chain Monte Carlo method MCMC principle and R language implementation | data sharing](/img/20/bb43ab1bc447b519c3b1de0f809b31.png)
[Video] Markov chain Monte Carlo method MCMC principle and R language implementation | data sharing

Atcoder beginer contest 261e / / bitwise thinking + DP

The migration of arm architecture to alsa lib and alsa utils is smooth

面试官问我:Mysql的存储引擎你了解多少?

Convolutional neural network model -- vgg-16 network structure and code implementation

Shell常用脚本:获取网卡IP地址
![[figure attack and Defense] backdoor attacks to graph neural networks (sacmat '21)](/img/d2/6be99fd194c66e4f60af38c6e52c93.png)
[figure attack and Defense] backdoor attacks to graph neural networks (sacmat '21)

【CTR】《Towards Universal Sequence Representation Learning for Recommender Systems》 (KDD‘22)

Shell common script: check whether a domain name and IP address are connected

arm架构移植alsa-lib和alsa-utils一路畅通
随机推荐
我的创作纪念日
【视频】马尔可夫链蒙特卡罗方法MCMC原理与R语言实现|数据分享
Pytorch creates its own dataset and loads the dataset
Basic knowledge of binary tree
Migrate PaloAlto ha high availability firewall to panorama
B tree and b+ tree
Introduction and features of numpy (I)
My creation anniversary
【GCN-CTR】DC-GNN: Decoupled GNN for Improving and Accelerating Large-Scale E-commerce Retrieval WWW22
yum和vim须掌握的常用操作
Programmer growth chapter 27: how to evaluate requirements priorities?
Redis visualizer RDM installation package sharing
若依如何实现用户免密登录配置方法?
Azure Devops(十四) 使用Azure的私有Nuget仓库
0715RHCSA
vim基础操作汇总
【GCN-RS】Towards Representation Alignment and Uniformity in Collaborative Filtering (KDD‘22)
Vim技巧:永远显示行号
Blindly expanding the scale of the meta universe has deviated from the development logic of the meta universe
【AI4Code】《Unified Pre-training for Program Understanding and Generation》 NAACL 2021