当前位置:网站首页>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;
}
};
边栏推荐
- Shell common script: judge whether the file of the remote host exists
- B树和B+树
- 0717RHCSA
- 0717RHCSA
- QingChuang technology joined dragon lizard community to build a new ecosystem of intelligent operation and maintenance platform
- mysql函数汇总之日期和时间函数
- How to use causal inference and experiments to drive user growth| July 28 tf67
- Excel添加按键运行宏
- Excel import and export source code analysis
- Mlx90640 infrared thermal imager temperature sensor module development notes (V)
猜你喜欢

arm架构移植alsa-lib和alsa-utils一路畅通

【GCN-CTR】DC-GNN: Decoupled GNN for Improving and Accelerating Large-Scale E-commerce Retrieval WWW22

Convolutional neural network model -- lenet network structure and code implementation

Based on Baiwen imx6ull_ Pro development board transplants LCD multi touch driver (gt911)

R语言GLM广义线性模型:逻辑回归、泊松回归拟合小鼠临床试验数据(剂量和反应)示例和自测题

G027-OP-INS-RHEL-04 RedHat OpenStack 创建自定义的QCOW2格式镜像

Docker learning - redis cluster -3 master and 3 slave - capacity expansion - capacity reduction building

从输入网址到网页显示

【GCN-RS】Learning Explicit User Interest Boundary for Recommendation (WWW‘22)

OAuth,JWT ,OIDC你们搞得我好乱啊
随机推荐
massCode 一款优秀的开源代码片段管理器
0717RHCSA
C # basic learning (XXIII)_ Forms and events
Emqx cloud update: more parameters are added to log analysis, which makes monitoring, operation and maintenance easier
ThreadLocal&Fork/Join
Oran special series-21: major players (equipment manufacturers) and their respective attitudes and areas of expertise
Concurrent programming - memory model JMM
B树和B+树
Online Learning and Pricing with Reusable Resources: Linear Bandits with Sub-Exponential Rewards: Li
Cyberspace Security penetration attack and defense 9 (PKI)
Programmer growth chapter 27: how to evaluate requirements priorities?
Introduction to jupyter notebook
0713RHCSA
ESP32-C3 基于Arduino框架下Blinker点灯控制10路开关或继电器组
【GCN-CTR】DC-GNN: Decoupled GNN for Improving and Accelerating Large-Scale E-commerce Retrieval WWW22
央行数研所穆长春:数字人民币可控匿名是维护公众利益和金融安全的客观需要
并发编程之AQS
R语言GLM广义线性模型:逻辑回归、泊松回归拟合小鼠临床试验数据(剂量和反应)示例和自测题
0716RHCSA
Excel add key run macro