当前位置:网站首页>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
}
};
边栏推荐
猜你喜欢
![[SUCTF2018]followme](/img/63/9104f9c8bd24937b0fc65053efec96.png)
[SUCTF2018]followme

618再次霸榜的秘密何在?耐克最新财报给出答案

618 what is the secret of dominating the list again? Nike's latest financial report gives the answer

2022-06-17

JSP webshell free -- webshell free

Analysis of hot spots in AI technology industry

UVM learning - build a simple UVM verification platform

LabVIEW为什么浮点数会丢失精度

华为AppLinking中统一链接的创建和使用

13. Semaphore critical zone protection
随机推荐
华为AppLinking中统一链接的创建和使用
Easyexcel, a concise, fast and memory saving excel processing tool
【快应用】Win7系统使用华为IDE无法运行和调试项目
首份中国企业敏捷实践白皮书发布| 附完整下载
[visual studio] visual studio 2019 community version cmake development environment installation (download | install relevant components | create compilation execution project | error handling)
【快应用】text组件里的文字很多,旁边的div样式会被拉伸如何解决
如何使用IDE自动签名调试鸿蒙应用
MySQL数据库远程访问权限设置
Common methods of JS array
二叉树专题--AcWing 3384. 二叉树遍历(已知先序遍历 边建树 边输出中序遍历)
【AGC】如何解决事件分析数据本地和AGC面板中显示不一致的问题?
【AI应用】海康威视iVMS-4200软件安装
Use WinDbg to statically analyze dump files (summary of practical experience)
[TS] 1368 seconds understand typescript generic tool types!
二叉树专题--AcWing 18. 重建二叉树(利用前、中序遍历,构建二叉树)
QT学习日记7——QMainWindow
Oracle 笔记
软件产品管理系统有哪些?12个最佳产品管理工具盘点
Start class, data analysis, high salary training plan, elite class
2022-06-17