当前位置:网站首页>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;
}
};
边栏推荐
- vim基础操作汇总
- Arrays常用方法
- Based on Baiwen imx6ull_ Ap3216 experiment driven by Pro development board
- Date and time function of MySQL function summary
- mujoco_py中文文档
- 为提高效率使用ParallelStream竟出现各种问题
- JS Array indexOf includes sort() 冒号排序 快速排序 去重和随机样本 random
- Discussion on principle and application technology of MLIR
- [figure attack and Defense] backdoor attacks to graph neural networks (sacmat '21)
- 卷积神经网络模型之——LeNet网络结构与代码实现
猜你喜欢

Django 2 ----- 数据库与Admin

0716RHCSA

Concurrent programming - memory model JMM

0710RHCSA

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

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

OAuth, JWT, oidc, you mess me up
![[ai4code final chapter] alphacode: competition level code generation with alphacode (deepmind)](/img/05/86eed30a7c063beace400a005e4a4c.png)
[ai4code final chapter] alphacode: competition level code generation with alphacode (deepmind)

Common operations for Yum and VIM

Docekr learning - MySQL 8 master-slave replication setup deployment
随机推荐
Convolutional neural network model -- lenet network structure and code implementation
【视频】马尔可夫链原理可视化解释与R语言区制转换MRS实例|数据分享
Excel record macro
二叉树基本知识
Concurrent programming - memory model JMM
Design and principle of thread pool
Excel添加按键运行宏
Arrays常用方法
【重温SSM框架系列】15 - SSM系列博文总结【SSM杀青篇】
OAuth, JWT, oidc, you mess me up
手写jdbc的使用步骤?
[Video] visual interpretation of Markov chain principle and Mrs example of R language region conversion | data sharing
Based on Baiwen imx6ull_ Pro development board transplants LCD multi touch driver (gt911)
VIM tip: always show line numbers
Peripheral system calls SAP's webapi interface
C # basic learning (XXIII)_ Forms and events
Blindly expanding the scale of the meta universe has deviated from the development logic of the meta universe
Uncaught SyntaxError: Octal literals are not allowed in strict mode.
Generate SQL script file by initializing the latest warehousing time of vehicle attributes
面试官问我:Mysql的存储引擎你了解多少?