当前位置:网站首页>Leetcode 113. path sum II
Leetcode 113. path sum II
2022-07-25 13:27:00 【LuZhouShiLi】
Leetcode 113. The sum of the paths II
subject
Give you the root node of the binary tree root And an integer target and targetSum , Find out all From the root node to the leaf node The path sum is equal to the given target sum .
Leaf node A node without children .
Ideas
- First create a vector<vector> resuLt Result array , To save the path path Array
- The first sequence traversal , First, judge whether the left and right subtrees of the current node are empty ,count Is it 0, If it is , It shows that a path has been found , Store the path
- Recursively traverses the left subtree , First, save the current node path in , Counter count Subtract the current node value , Then recursively traverse , If the return is unsuccessful , Directly undo the last stored result .
Code
/** * 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)
{
// Leaf node encountered also count = 0 Save the path
if(!cur->left && !cur->right && count == 0)
{
result.push_back(path);
return;
}
// Leaf node encountered and , And did not find the right side , Go straight back to
if(!cur->left && !cur->right)
{
return;
}
if(cur->left)
{
// Recursively traverses the left subtree
path.push_back(cur->left->val);// Save the current node value
count -= cur->left->val;
traversal(cur->left,count); // recursive
count += cur->left->val;// to flash back
path.pop_back();// to flash back
}
if(cur->right)
{
// Recursively traverses the right subtree
path.push_back(cur->right->val);
count -= cur->right->val;
traversal(cur->right,count);// recursive
count += cur->right->val;// to flash back
path.pop_back();// to flash back
}
return;
}
vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
result.clear();
path.clear();
if(root == NULL)
{
return result;
}
path.push_back(root->val);// Send the root node into the path
traversal(root,targetSum - root->val);
return result;
}
};
边栏推荐
- Summary of Niuke forum project deployment
- 【AI4Code】《Pythia: AI-assisted Code Completion System》(KDD 2019)
- 0716RHCSA
- Numpy快速入门
- Mujoco+spinningup for intensive learning training quick start
- [Video] visual interpretation of Markov chain principle and Mrs example of R language region conversion | data sharing
- Online Learning and Pricing with Reusable Resources: Linear Bandits with Sub-Exponential Rewards: Li
- Convolutional neural network model -- lenet network structure and code implementation
- HTTP cache tongtianpian, there may be something you want
- 安装mujoco报错:distutils.errors.DistutilsExecError: command ‘gcc‘ failed with exit status 1
猜你喜欢

Redis可视化工具RDM安装包分享

【GCN-RS】Region or Global? A Principle for Negative Sampling in Graph-based Recommendation (TKDE‘22)

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

Concurrent programming - memory model JMM
TCP的拥塞控制

二叉树基本知识
![[review SSM framework series] 15 - Summary of SSM series blog posts [SSM kill]](/img/fb/6ca8e0eb57c76c515e2aae68e9e549.png)
[review SSM framework series] 15 - Summary of SSM series blog posts [SSM kill]

cv2.resize函数报错:error: (-215:Assertion failed) func != 0 in function ‘cv::hal::resize‘

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

uniapp处理后台传输图片
随机推荐
hcip第九天笔记
【服务器数据恢复】HP EVA服务器存储RAID信息断电丢失的数据恢复
【GCN-RS】Towards Representation Alignment and Uniformity in Collaborative Filtering (KDD‘22)
Date and time function of MySQL function summary
pytorch创建自己的Dataset加载数据集
Prepare for 2022 csp-j1 2022 csp-s1 preliminaries video set
【重温SSM框架系列】15 - SSM系列博文总结【SSM杀青篇】
Convolutional neural network model -- alexnet network structure and code implementation
Leetcode 113. 路径总和 II
【AI4Code】《Unified Pre-training for Program Understanding and Generation》 NAACL 2021
Peripheral system calls SAP's webapi interface
stable_baselines快速入门
Mu Changchun, data Research Institute of the central bank: controllable anonymity of digital RMB is an objective need to safeguard public interests and financial security
Excel import and export source code analysis
Mlx90640 infrared thermal imager temperature sensor module development notes (V)
Any time, any place, super detective, seriously handle the case!
Excel record macro
QGIS加载在线地图:高德、天地图等
并发编程之AQS
央行数研所穆长春:数字人民币可控匿名是维护公众利益和金融安全的客观需要