当前位置:网站首页>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;
}
};
边栏推荐
- The world is exploding, and the Google server has collapsed
- Migrate PaloAlto ha high availability firewall to panorama
- Django 2 ----- database and admin
- C # basic learning (XXIII)_ Forms and events
- 【AI4Code】《Pythia: AI-assisted Code Completion System》(KDD 2019)
- How to realize the configuration method of user password free login?
- Summary of Niuke forum project deployment
- The programmer's father made his own AI breast feeding detector to predict that the baby is hungry and not let the crying affect his wife's sleep
- Memory layout of program
- pytorch创建自己的Dataset加载数据集
猜你喜欢

Excel add key run macro

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

Shell常用脚本:判断远程主机的文件是否存在

Substance designer 2021 software installation package download and installation tutorial

0715RHCSA

Based on Baiwen imx6ull_ Ap3216 experiment driven by Pro development board

How to understand metrics in keras

安装mujoco报错:distutils.errors.DistutilsExecError: command ‘gcc‘ failed with exit status 1

Excel record macro

卷积神经网络模型之——GoogLeNet网络结构与代码实现
随机推荐
The interviewer asked me: how much do you know about MySQL's storage engine?
0715RHCSA
The programmer's father made his own AI breast feeding detector to predict that the baby is hungry and not let the crying affect his wife's sleep
[six articles talk about scalablegnn] around www 2022 best paper PASCA
Memory layout of program
Docker learning - redis cluster -3 master and 3 slave - capacity expansion - capacity reduction building
外围系统调用SAP的WebAPI接口
Convolutional neural network model -- googlenet network structure and code implementation
mujoco+spinningup进行强化学习训练快速入门
G027-OP-INS-RHEL-04 RedHat OpenStack 创建自定义的QCOW2格式镜像
R language GLM generalized linear model: logistic regression, Poisson regression fitting mouse clinical trial data (dose and response) examples and self-test questions
0713RHCSA
cv2.resize函数报错:error: (-215:Assertion failed) func != 0 in function ‘cv::hal::resize‘
Programmer growth chapter 27: how to evaluate requirements priorities?
Any time, any place, super detective, seriously handle the case!
卷积神经网络模型之——AlexNet网络结构与代码实现
B tree and b+ tree
一味地做大元宇宙的规模,已经背离了元宇宙本该有的发展逻辑
【视频】马尔可夫链原理可视化解释与R语言区制转换MRS实例|数据分享
【GCN-RS】Learning Explicit User Interest Boundary for Recommendation (WWW‘22)