当前位置:网站首页>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
}
};
边栏推荐
- 二叉树专题--洛谷 P1229 遍历问题(乘法原理 已知前、后序遍历求中序遍历个数)
- PCL eigen introduction and simple use
- Filtering of PCL
- 快应用中实现自定义抽屉组件
- AttributeError: type object ‘Image‘ has no attribute ‘fromarray‘
- Common methods of JS array
- 二叉树专题--洛谷 P3884 [JLOI2009]二叉树问题(dfs求二叉树深度 bfs求二叉树宽度 dijkstra求最短路)
- MySQL数据库远程访问权限设置
- Oracle notes
- JSP webshell免杀——webshell免杀
猜你喜欢

14. Code implementation of semaphore

MongoDB 学习整理(条件操作符,$type 操作符,limit()方法,skip() 方法 和 sort() 方法)

Open the encrypted SQLite method with sqlcipher

Dialogue Wu Gang: why do I believe in the rise of "big country brands"?

Thanos Receiver

从.bag文件中读取并保存.jpg图片和.pcd点云

使用华为性能管理服务,按需配置采样率

二叉树专题--AcWing 1497. 树的遍历(利用后、中序遍历,构建二叉树)

二叉树专题--AcWing 18. 重建二叉树(利用前、中序遍历,构建二叉树)

【快应用】text组件里的文字很多,旁边的div样式会被拉伸如何解决
随机推荐
C#中索引器
Common methods of JS array
Kustomize user manual
PCL Eigen介绍及简单使用
如何使用IDE自动签名调试鸿蒙应用
【AI应用】海康威视iVMS-4200软件安装
Beautiful and intelligent, Haval H6 supreme+ makes Yuanxiao travel safer
SUS系统可用性量表
js数组常用方法
Operator-1 first acquaintance with operator
Shell programming 01_ Shell foundation
AttributeError: type object ‘Image‘ has no attribute ‘fromarray‘
Filtering of PCL
从MediaRecord录像中读取H264参数
12.进程同步与信号量
Win11 arm系统配置.net core环境变量
JVM之垃圾回收器
二叉树专题--AcWing 19. 二叉树的下一个节点(找树中节点的后继)
PCL projection point cloud
Oracle 笔记