当前位置:网站首页>力扣------路径总和 III
力扣------路径总和 III
2022-07-06 21:00:00 【qq_37760746】
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */
int count;
void sum(struct TreeNode* root, int targetSum,long int nowsum){
if(nowsum==targetSum){
count++;
}
if(root->left){
nowsum=nowsum+root->left->val;
sum(root->left,targetSum,nowsum);
nowsum=nowsum-root->left->val;
}
if(root->right){
nowsum=nowsum+root->right->val;
sum(root->right,targetSum,nowsum);
nowsum=nowsum-root->right->val;
}
}
void dfs(struct TreeNode* root, int targetSum){
long int nowsum=0;
nowsum=nowsum+root->val;
sum(root, targetSum,nowsum);
if(root->left){
dfs(root->left,targetSum);
}
if(root->right){
dfs(root->right,targetSum);
}
}
int pathSum(struct TreeNode* root, int targetSum){
count=0;
if(!root){
return count;
}
dfs(root,targetSum);
return count;
}
边栏推荐
- 20. (ArcGIS API for JS) ArcGIS API for JS surface collection (sketchviewmodel)
- ubuntu20安装redisjson记录
- 预处理——插值
- 小程序能运行在自有App中,且实现直播和连麦?
- 浅谈网络安全之文件上传
- A 股指数成分数据 API 数据接口
- 密码学系列之:在线证书状态协议OCSP详解
- Mathematical induction and recursion
- VHDL实现任意大小矩阵加法运算
- Optimization cases of complex factor calculation: deep imbalance, buying and selling pressure index, volatility calculation
猜你喜欢
随机推荐
小程序能运行在自有App中,且实现直播和连麦?
[dream database] add the task of automatically collecting statistical information
[safe office and productivity application] Shanghai daoning provides you with onlyoffice download, trial and tutorial
大白话高并发(二)
Stored procedures and functions (MySQL)
【DPDK】dpdk样例源码解析之三:dpdk-l3fwd_001
Experience design details
About Estimation Statistics
codeforces每日5题(均1700)-第七天
枚举通用接口&枚举使用规范
MySQL的索引
Can the applet run in its own app and realize live broadcast and connection?
[leetcode] 450 and 98 (deletion and verification of binary search tree)
U.S. Air Force Research Laboratory, "exploring the vulnerability and robustness of deep learning systems", the latest 85 page technical report in 2022
未来发展路线确认!数字经济、数字化转型、数据...这次会议很重要
VHDL implementation of arbitrary size matrix addition operation
Tencent cloud native database tdsql-c was selected into the cloud native product catalog of the Academy of communications and communications
SSL certificate deployment
Introduction to opensea platform developed by NFT trading platform (I)
When QT uses qtooltip mouse to display text, the picture of the button will also be displayed and the prompt text style will be modified