当前位置:网站首页>437. 路径总和 III
437. 路径总和 III
2022-04-22 06:16:00 【yinhua405】
给定一个二叉树的根节点 root ,和一个整数 targetSum ,求该二叉树里节点值之和等于 targetSum 的 路径 的数目。
路径 不需要从根节点开始,也不需要在叶子节点结束,但是路径方向必须是向下的(只能从父节点到子节点)。
class Solution {
public:
void f(TreeNode* root, int targetSum,vector<int>sum,int &count)
{
if(nullptr ==root)
{
return;
}
vector<int> tmp;
for(auto it:sum)
{
tmp.push_back(it+root->val);
if(it+root->val == targetSum)
{
count++;
}
}
sum = tmp;
sum.push_back(root->val);
if(root->val == targetSum)
{
count++;
}
f(root->left, targetSum,sum,count);
f(root->right, targetSum,sum,count);
}
int pathSum(TreeNode* root, int targetSum) {
int ret=0;
vector<int>sum;
f(root,targetSum,sum,ret);
return ret;
}
};
版权声明
本文为[yinhua405]所创,转载请带上原文链接,感谢
https://blog.csdn.net/yinhua405/article/details/121668838
边栏推荐
- Anaconda安装与使用
- ROS installation and foundation and introduction
- Wechat browser cannot save cookies for a long time
- The only storage area in the JVM where GC and oom will not occur
- [DRC RTSTAT-1] Unrouted nets: 1 net(s) are unrouted
- Interviewers often ask about the general process and special circumstances of object allocation
- (六) Sql Server的DCL丶DML语法
- 【数论】素数(五):梅森素数(Lucas_Lehmer判定法)
- Build ES6 development environment and compile in real time
- [number theory] prime number (4): decomposition of numbers (Pollard rho)
猜你喜欢

Flutter环境搭建踩坑过程

Raspberry Pi 4b

Complete a student information management system and systematically practice object-oriented, function, string and other knowledge. Realize the comprehensive application of knowledge. Use classes, fun

浅谈时间复杂度与空间复杂度

链表习题详解

Pixhawk4+Up Board / NUC Implement VIO By Deploying T265

(2) Basic configuration of SQL server and connection to SQL server using Navicat

Binary tree chain structure operation leetcode + Niuke (detailed explanation)

C语言 | 快速排序
![. net learning notes - about Net core (1) [. NETCORE's project structure, five ways to transfer values to pages, and the use of log4net and NLog]](/img/c8/e7579543d7911a907ae19f357478d8.png)
. net learning notes - about Net core (1) [. NETCORE's project structure, five ways to transfer values to pages, and the use of log4net and NLog]
随机推荐
队列(详解)——手撕队列习题
ASP. Net daily development notes ----- send email
机械设计知识点规划
ROS安装及基础与入门
我们常说的栈帧的内部结构是什么样的呢?
使用Feign做远程调用的注意点
C语言 | 指针
C语言 | 位运算符
a5 transceiver 信号vod和预加重调整关系
虚拟机磁盘空间缩小
. net daily thoughts - compare two JSON files and get data with the same tag but different values
LaTex中添加作者照片和作者简介
Define an abstract role class with name, age, gender, hobbies and other member variables. It is required to hide all variables as far as possible (private if possible), and then read and write each va
Written examination for summer internship of meituan spring recruitment -- 20220312
【数论】素数(一):基本概念、性质、猜想、定理
【题解】洛谷P6186 [NOI Online #1 提高组] 冒泡排序:【冒泡排序】与【逆序对】问题
微信浏览器无法长期保存cookie
SQL Server quick start
Beyond Compare“授权密钥已被吊销”的解决办法
顺序表之高速缓存命中率