当前位置:网站首页>Special topic of binary tree -- acwing 47 Path with a certain value in binary tree (preorder traversal)
Special topic of binary tree -- acwing 47 Path with a certain value in binary tree (preorder traversal)
2022-07-02 10:58:00 【Morgannr】

The question :
Enter a binary tree and an integer , Print out Sum of node values in a binary tree by Input integer Of All paths .
from Root node of tree Start Down to the leaf node Node passed Form a path .
Ensure the node value in the tree No less than 0.
Ideas :
Traverse from top to bottom , When traversing the leaf node , Judge whether the current path weight sum is equal to the target value .
if , Record the answer .
If it is not , No operation
Code :
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {
}
* };
*/
class Solution {
public:
// take Answer array ans As a global variable in the class , It is more convenient to record the answers .
vector<vector<int>> res;
vector<int> tmp;
vector<vector<int>> findPath(TreeNode* root, int sum) {
if(!root) return res;
dfs(root, 0, sum);
return res;
}
void dfs(TreeNode* rt, int sm, int sum)
{
if (sm > sum) return ;
tmp.push_back(rt->val);// The former sequence traversal , Root left and right , Advanced vector
sm += rt->val;
if (!rt->left && !rt->right)// leaf node
{
if (sm == sum)
{
res.push_back(tmp);
}
}
// The former sequence traversal , After traversing the root node above, the left 、 Right son
if (rt->left) dfs(rt->left, sm, sum);
if (rt->right) dfs(rt->right, sm, sum);
// Because you have to traverse other branches , Therefore, the scene must be restored
tmp.pop_back();
sm -= rt->val; // It doesn't matter ,sum What comes in is scalar , It's not the address
}
};
边栏推荐
- (5) Gear control setting of APA scene construction
- [SUCTF2018]followme
- Disassembling Meitu SaaS: driving the plane to change the engine
- Oracle notes
- 洛谷 P1892 [BOI2003]团伙(并查集变种 反集)
- MYSQL关键字
- js数组常用方法
- 二叉树专题--AcWing 1497. 树的遍历(利用后、中序遍历,构建二叉树)
- 二叉树专题--AcWing 19. 二叉树的下一个节点(找树中节点的后继)
- 12. Process synchronization and semaphore
猜你喜欢

二叉树专题--AcWing 3540. 二叉搜索树建树(实用板子 构建二叉搜索树 并输出前、中、后序遍历)

HDU1234 开门人和关门人(水题)

Retrofit's callback hell is really vulnerable in kotlin synergy mode!

Hdu1228 a + B (map mapping)

Kustomize使用手册

Introduction to MySQL 8 DBA foundation tutorial

【AppLinking实战案例】通过AppLinking分享应用内图片

集成学习概览

Win11 arm系统配置.net core环境变量

Dialogue Wu Gang: why do I believe in the rise of "big country brands"?
随机推荐
How to get the password of cpolar?
二叉树专题--P1030 [NOIP2001 普及组] 求先序排列
学习open62541 --- [66] UA_String的生成方法
【快应用】text组件里的文字很多,旁边的div样式会被拉伸如何解决
Use of vscode tool
AI技术产业热点分析
AppGallery Connect场景化开发实战—图片存储分享
nodejs+express+mysql简单博客搭建
大华设备播放过程中设置播放速度
二叉树专题--AcWing 19. 二叉树的下一个节点(找树中节点的后继)
Filtering of PCL
4. Random variables
MySQL数据库远程访问权限设置
Operator-1 first acquaintance with operator
二叉树专题--AcWing 18. 重建二叉树(利用前、中序遍历,构建二叉树)
快应用中实现自定义抽屉组件
AttributeError: type object ‘Image‘ has no attribute ‘fromarray‘
Hdu1236 ranking (structure Sorting)
洛谷 P5536 【XR-3】核心城市(贪心 + 树形 dp 寻找树的中心)
V2X-Sim数据集(上海交大&纽约大学)