当前位置:网站首页>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
}
};
边栏推荐
- Disassembling Meitu SaaS: driving the plane to change the engine
- 2022-06-17
- UVM - configuration mechanism
- HDU1228 A + B(map映射)
- 【AGC】如何解决事件分析数据本地和AGC面板中显示不一致的问题?
- 【ARK UI】HarmonyOS ETS的启动页的实现
- Thanos Receiver
- In the face of uncertainty, the role of supply chain
- The nanny level tutorial of flutter environment configuration makes the doctor green to the end
- 二叉树专题--AcWing 18. 重建二叉树(利用前、中序遍历,构建二叉树)
猜你喜欢
随机推荐
华为游戏初始化init失败,返回错误码907135000
Hdu1234 door opener and door closer (water question)
What are the popular frameworks for swoole in 2022?
6种单例模式的实现方式
What is the significance of the college entrance examination
二叉树专题--AcWing 1589. 构建二叉搜索树
Win11 arm系统配置.net core环境变量
PCL projection point cloud
1287_ Implementation analysis of prvtaskistasksuspended() interface in FreeRTOS
AttributeError: type object ‘Image‘ has no attribute ‘fromarray‘
Leetcode+ 76 - 80 storm search topic
UVM factory mechanism
【AI应用】海康威视iVMS-4200软件安装
二叉树专题--AcWing 1497. 树的遍历(利用后、中序遍历,构建二叉树)
Internet News: Tencent conference application market was officially launched; Soul went to Hong Kong to submit the listing application
Hdu1228 a + B (map mapping)
K-d tree and octree of PCL
[visual studio] visual studio 2019 community version cmake development environment installation (download | install relevant components | create compilation execution project | error handling)
快应用中实现自定义抽屉组件
Disassembling Meitu SaaS: driving the plane to change the engine