当前位置:网站首页>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;
}
};
边栏推荐
- arm架构移植alsa-lib和alsa-utils一路畅通
- Seven lines of code made station B crash for three hours, but "a scheming 0"
- 并发编程之阻塞队列
- 二叉树基本知识
- Machine learning strong foundation program 0-4: popular understanding of Occam razor and no free lunch theorem
- mujoco_py中文文档
- 基于百问网IMX6ULL_PRO开发板驱动AP3216实验
- [Video] visual interpretation of Markov chain principle and Mrs example of R language region conversion | data sharing
- 【GCN-CTR】DC-GNN: Decoupled GNN for Improving and Accelerating Large-Scale E-commerce Retrieval WWW22
- 0716RHCSA
猜你喜欢

【AI4Code】《Pythia: AI-assisted Code Completion System》(KDD 2019)

G027-OP-INS-RHEL-04 RedHat OpenStack 创建自定义的QCOW2格式镜像
![Detailed explanation of the training and prediction process of deep learning [taking lenet model and cifar10 data set as examples]](/img/70/2b5130be16d7699ef7db58d9065253.png)
Detailed explanation of the training and prediction process of deep learning [taking lenet model and cifar10 data set as examples]

【AI4Code】CodeX:《Evaluating Large Language Models Trained on Code》(OpenAI)

卷积神经网络模型之——VGG-16网络结构与代码实现

Jupyter Notebook介绍

Design and principle of thread pool

Excel add key run macro

R language GLM generalized linear model: logistic regression, Poisson regression fitting mouse clinical trial data (dose and response) examples and self-test questions

Generate SQL script file by initializing the latest warehousing time of vehicle attributes
随机推荐
int数组获取重复数据
Redis visualizer RDM installation package sharing
QingChuang technology joined dragon lizard community to build a new ecosystem of intelligent operation and maintenance platform
yum和vim须掌握的常用操作
网络空间安全 渗透攻防9(PKI)
Business visualization - make your flowchart'run'(3. Branch selection & cross language distributed operation node)
并发编程之并发工具集
[figure attack and Defense] backdoor attacks to graph neural networks (sacmat '21)
B tree and b+ tree
Atcoder beginer contest 261e / / bitwise thinking + DP
卷积神经网络模型之——GoogLeNet网络结构与代码实现
MLX90640 红外热成像仪测温传感器模块开发笔记(五)
备战2022 CSP-J1 2022 CSP-S1 初赛 视频集
Docekr学习 - MySQL8主从复制搭建部署
Convolutional neural network model -- alexnet network structure and code implementation
ThreadLocal&Fork/Join
【CSDN 年终总结】结束与开始,一直在路上—— “1+1=王”的2021总结
0716RHCSA
Seven lines of code made station B crash for three hours, but "a scheming 0"
Convolutional neural network model -- vgg-16 network structure and code implementation