Leetcode---Path Sum II

基于STM32L4XX、HAL库的GC9307 TFT 液晶驱动IC驱动程序设计 GC9307是一款常用的TFT液晶驱动IC,支持240x320分辨率的RGB接口显示屏。(Master Out Slave In): 连接到STM32L4的SPI数据输出引脚(如PA7)(Serial Clock): 连接到STM32L4的SPI时钟引脚(如PA5)(Data/Command): 连接到STM32L4的GPIO引脚(如PA3)(Chip Select): 连接到STM32L4的GPIO引脚(如PA4)(Reset): 连接到STM32L4的GPIO引脚(如PA2): 分别连接到电源和地。 阅读详情

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.

For example:
Given the below binary tree and sum = 22,
              5
             / \
            4   8
           /   / \
          11  13  4
         /  \    / \
        7    2  5   1

return

[
   [5,4,11,2],
   [5,8,4,5]
]



Show Tags
Have you met this question in a real interview?

这道题关键的地方在于如何传递一个vector,让它在递归中不停的存数据,递归完了之后,又能弹出之前的数据。

所以,一个关键的技巧就是不传引用,而是传值。这样,每次递归的时候,实际上都会在栈上生成一个新的参数,递归结束的时候,原传入参数没有受到任何影响。再回到上一层,就像是弹出了。

好像是在说天书,好吧,看下面两个实现,一个是用引用类型的,一个是用值类型的,都Accept了
引用类型:

  1. vector<vector<int> > result;
  2. void pathSumHelper(TreeNode *root, int sum, vector<int>& tmp){
  3.     if(root==NULL)
  4.         return;
  5.     if(root->left==NULL &&root->right==NULL){
  6.         if(sum==root->val){
  7.             tmp.push_back(root->val);
  8.             result.push_back(tmp);
  9.         }
  10.         return;
  11.     }
  12.     sum-=root->val;
  13.     tmp.push_back(root->val);
  14.     vector<int> tmp2(tmp);
  15.     vector<int> tmp3(tmp);
  16.     pathSumHelper(root->left,sum,tmp2);
  17.     pathSumHelper(root->right,sum,tmp3);
  18.     sum+=root->val;
  19.     tmp.pop_back();

  20. }
  21. vector<vector<int> > pathSum(TreeNode *root, int sum) {
  22.     vector<int> tmp;
  23.     pathSumHelper(root,sum,tmp);
  24.     return result;
  25. }
其实就是在传入的时候copy了一把。但是注意,每个函数都要copy一把,否则又会错。

然后是值类型的:

  1. vector<vector<int> > result;
  2. void pathSumHelper(TreeNode *root, int sum, vector<int> tmp){
  3.     if(root==NULL)
  4.         return;
  5.     if(root->left==NULL &&root->right==NULL){
  6.         if(sum==root->val){
  7.             tmp.push_back(root->val);
  8.             result.push_back(tmp);
  9.         }
  10.         return;
  11.     }
  12.     sum-=root->val;
  13.     tmp.push_back(root->val);
  14.     pathSumHelper(root->left,sum,tmp);
  15.     pathSumHelper(root->right,sum,tmp);
  16.     sum+=root->val;
  17.     tmp.pop_back();

  18. }
  19. vector<vector<int> > pathSum(TreeNode *root, int sum) {
  20.     vector<int> tmp;
  21.     pathSumHelper(root,sum,tmp);
  22.     return result;
  23. }

非常整齐的代码,但是因为是值传递,第17行 tmp . pop_back ( ) ;可以不要,因为函数结束了tmp其实也销毁了。

最近又写了一个

vector<vector<int> > result;
void help(TreeNode *root, int& num, int &sum, vector<int>& temp){
    if(root==NULL){
    }
    else if(root->left==NULL && root->right==NULL){
        num+=root->val;
        temp.push_back(root->val);
        if(num==sum)
            result.push_back(temp);
        temp.pop_back();
        num-=root->val;
    }
    else{
        temp.push_back(root->val);
        num+=root->val;
        help(root->left,num,sum,temp);
        help(root->right,num,sum,temp);
        num-=root->val;
        temp.pop_back();
    }
}
vector<vector<int> > pathSum(TreeNode *root, int sum) {
    vector<int> temp;
    int num=0;
    help(root,num,sum,temp);
    return result;
}


GPU:NVIDIA GPU的发展历史,nvidia-smi的简介、安装、使用方法之详细攻略 GPU:NVIDIA GPU的发展历史,nvidia-smi的简介、安装、使用方法之详细攻略 目录 NVIDIA GPU的发展历史 nvidia-smi的简介 1、nvidia-smi是nvidia 的系统管理界面解释相关参数含义 2、NVIDIA系统管理接口——v388.73 3、支持产品: nvidia-smi的安装 nvidia-smi的使用方法 1、列出所有可用的 NVIDIA 设备命令 2、nvidia-smi的基础命令用法 NVIDIA GPU的发展历 阅读详情

相关推荐

Ubuntu18.04环境搭建与疑难解析:从零复现YOLO-6D姿态估计算法

本文详细介绍了在Ubuntu18.04环境下从零开始复现YOLO-6D姿态估计算法的完整流程,包括系统配置、Conda环境搭建、PyTorch安装、依赖项管理、源代码获取、数据集准备以及训练验证过程。针对Singleshotpose算法的特殊需求,提供了详细的疑难解析和性能优化技巧,帮助开发者高效完成算法复现和应用部署。

weixin_30369041的博客 455

Path Sum II -- LeetCode

Path Sum II -- LeetCode

weixin_43418664的博客 133

CloudCompare实战:5分钟搞定点云数据预处理(含常见格式转换技巧)

本文提供了一套基于CloudCompare的5分钟点云数据预处理实战指南。文章详细讲解了如何利用这款开源工具,快速完成点云数据的格式转换(如LAS转PLY)、统计离群值降噪、体素网格下采样以及交互式裁剪等核心操作,帮助用户高效清理和规整杂乱数据,为后续分析建模奠定基础。

weixin_29207533的博客 477

【LeetCode with Python】 Path Sum II

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / \ 4 8 / / \ 11 13 4

XNERV SURVEYS 2053

机器人开发路线

通过这个路线图,你可以在6个月内完成从原型到试产的全流程。建议初期采用敏捷开发模式,每两周迭代一个核心功能模块。在硬件集成遇到瓶颈时,推荐参考。

woody218的博客 745

Fluent 2.5D动网格重构实战:涡旋压缩机复杂运动模拟指南

本文详细解析了Fluent 2.5D动网格重构技术在涡旋压缩机复杂运动模拟中的应用。通过对比传统方法,2.5D重构显著提升了计算效率和稳定性,特别适用于柱体类几何的网格处理。文章提供了从基础配置到参数优化的实战指南,包括关键设置步骤、常见问题排查及性能优化策略,帮助工程师有效解决动网格重构中的负体积和网格畸变问题。

weixin_28455801的博客 549

Leetcode 113. Path Sum II

Leetcode 113. Path Sum IIsource url题目描述Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum.For example: Given the below binary tree and sum = 22,

vincent_cheung 330

Dify插件离线部署全攻略:从下载到安装的完整流程

本文详细介绍了Dify插件离线部署的完整流程,针对企业内网等无网络环境需求。核心步骤包括在联网环境下载插件包、在特定Python 3.12.9环境下使用专用工具进行离线重打包,以及最终在离线Dify环境中安装验证。重点解决了离线安装的核心难题,并提供了环境配置、批量处理和故障排查等实用技巧,帮助用户实现Dify插件的顺利离线部署。

seed的博客 657

[LeetCode]113.Path Sum II

【题目】 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5

SmartSi 2044

基于Jira与Confluence的企业知识库AI助手构建实战

检索增强生成(RAG)技术通过结合信息检索与大型语言模型,有效解决了AI在专业领域知识不足的问题。其核心原理是将外部知识库(如企业文档)进行向量化存储,在用户提问时进行语义检索,并将相关上下文注入提示词中,从而生成精准、有据可依的回答。这一技术架构在工程实践中价值显著,它无需重新训练模型即可让AI掌握特定领域的专业知识,大幅提升了回答的准确性与实用性。典型的应用场景包括智能客服、企业内部知识问答以及代码辅助等。本文以开源项目`dev-kostiuk/jira-confluence-ai-context`为例

weixin_42520448的博客 367

射频收发机(RF Transceiver)

2,既然原来外差的思路是把声音频率的信号,通过混频搬移到高频电磁波频率,那对高频率信号放大的时候,是不是也可以先在比较低的射频频率进行放大,然后再通过频率搬移的方式,将放大后的信号搬移到高频率呢?频率搬移过程中,中间预先设定的固定射频频率被称为“中频”(中间频率,IF,Intermediate Frequency),由于这个频率超出了声音可听的范围,是“超声波”(supersonic),所以被命名为“超外差”(Super-Heterodyne)。混频后的信号在0频率附近,可能会发生DC偏移。

2301_77317229的博客 1122

Leetcode 113 Path Sum II(递归)

题目连接:Leetcode 113 Path Sum II解题思路:与 Leetcode 112 类似,搜索过程中记录路径并且在ans中保存可行方案。/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right;...

不慌不忙、不急不躁 287

thinkphp6/8 - 实现评论区发表评论、回复评论功能数据库表设计与整个逻辑思路过程分析,tp5 评论盖楼等功能(附带源码稍加改造即可使用)

tp5评论区数据库表设计,tp5发表回复评论功能设计,thinkphp5如何做评论功能,tp5 php评论区实现发表回复删除评论,tp5评论区盖楼源码及数据表设计思路,thinkphp评论功能详细教程,thinkphp实现评论与回复功能,thinkphp实现文章评论和回复评论功能,tp5实现评论回复功能,thinkphp如何实现评论区,tp5网站PC移动端h5怎么做评论区发表留言及回复功能,thinkphp实现评论区插件,thinkphp评论区相互发表回复功能详细教程,thinkphp有没有评论区的组件插件

✅ 精选推荐文章 3159

Leetcode 113. Path Sum II 路径和2 解题报告

1 解题思想这道题和Leetcode 112是同宗的,112只要求输出是否存在一个Case,而这题要求输出所有Case的路径。 方法基本是一样他,遍历到底的时候保存一下路径就好Leetcode 112. Path Sum 路径和 解题报告 2 原题Given a binary tree and a sum, find all root-to-leaf paths where each path's

MebiuW的专栏 2189

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

LeetCode | Unique Paths & II & Minimum Path Sum

62. Unique Paths 63. Unique Paths II 64. Minimum Path Sum

拖延癌患者自愈小记 388

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

LeetCode113. Path Sum II

LeetCode113. Path Sum II

bookman的博客 337

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

【leetcode刷题】62 63 64 unique path & unique path II & minimum path sum

原题链接 https://leetcode.com/problems/unique-paths/ https://leetcode.com/problems/unique-paths-ii/ https://leetcode.com/problems/minimum-path-sum/ 解题思路 这三道题比较类似,都是利用动态规划。 其中,unique path只需要把走到每一个格子里的方法数目加...

Arwen的博客 250

【LeetCode】113. Path Sum II 解题报告(Python)

【LeetCode】113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 题目地址:https://leetcode.com/problems/path-sum-ii/descript...

负雪明烛 2163

LeetCode Path Sum II

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.For example:Given the below binary tree and sum = 22, 5 / \

Coding for Dreams 2356

LeetCode - 113. Path Sum II

题目 LeetCode - 113. Path Sum II 题目链接 https://leetcode.com/problems/path-sum-ii/ 参考博客 解题思路 带路径的深度优先遍历。 解题源码 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode...

virgilshi的博客 158

LeetCode 113. Path Sum II

LeetCode 113. Path Sum IIDescription: Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum. Example: For example: Given the below binary tree and

My Blog 195

Leetcode 113. Path Sum II

113. Path Sum II Total Accepted:88034 Total Submissions:302190 Difficulty:Medium Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the...

weixin_30414245的博客 99

LeetCode力扣之113. Path Sum II

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.For example:Given the below binary tree and sum = 22, 5 / \ ...

LeeWei 272

LeetCode113. Path Sum II 找出二叉树中总值为sum的所有路径 #算法# 第十一周

原题如下 Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum. Note: A leaf is a node with no children. Example: Given the below binary tree and sum = 22, ...

Runner1st的博客 315

LeetCode: 113. Path Sum II

LeetCode: 113. Path Sum II 题目描述 Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum. For example: Given the below binary tree and sum = 22, ...

杨领well的专栏 599

[Leetcode] Path Sum I,II,III

112. Path Sum I: 点击打开链接 /** * Definition for a binary tree node. //判断是否有这样的路径满足和为sum * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(i

offerkkl的博客 435
上一篇: Leetcode---Validate Binary Search Tree
下一篇: Leetcode---Path Sum
我把葡萄酿成酒
博客等级 码龄12年 275粉丝 198原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值