Path Sum II - LeetCode
题目链接
注意点
- 不要访问空结点
解法
解法一:递归,DFS。每当DFS搜索到新节点时,都要保存该节点。而且每当找出一条路径之后,都将这个保存为一维vector的路径保存到最终结果二维vector中。并且,每当DFS搜索到子节点,发现不是路径和时,返回上一个结点时,需要把该节点从一维vector中移除。
/**
* 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:
void findPath(TreeNode* node,int sum,vector<int> temp,vector<vector<int>>& ret)
{
if(!node) return;
temp.push_back(node->val);
if(node->val == sum && !node->left && !node->right) ret.push_back(temp);
findPath(node->left,sum-(node->val),temp,ret);
findPath(node->right,sum-(node->val),temp,ret);
temp.pop_back();
}
vector<vector<int>> pathSum(TreeNode* root, int sum) {
vector<vector<int>> ret;
if(!root) return ret;
findPath(root,sum,{},ret);
return ret;
}
};

小结
Path Sum II - LeetCode的更多相关文章
- Path Sum II leetcode java
题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...
- Path Sum II——LeetCode
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)
LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...
- Leetcode: mimimum depth of tree, path sum, path sum II
思路: 简单搜索 总结: dfs 框架 1. 需要打印路径. 在 dfs 函数中假如 vector 变量, 不用 & 修饰的话就不需要 undo 2. 不需要打印路径, 可设置全局变量 ans ...
- [Leetcode Week14]Path Sum II
Path Sum II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/path-sum-ii/description/ Description Giv ...
- 【LeetCode】113. Path Sum II 解题报告(Python)
[LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- [leetcode]Path Sum II
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
- 【leetcode】Path Sum II
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
随机推荐
- SE93 创建参数事务
(1)SE93 输入新的事务码名称,点击创建按钮 (2)输入事务码描述,选择第五项: Transaction with parameters(parameter transaction) (3)在事务 ...
- 20155318 《网络攻防》Exp5 MSF基础应用
20155318 <网络攻防>Exp5 MSF基础应用 基础问题 用自己的话解释什么是exploit,payload,encode? exploit就相当于是载具,将真正要负责攻击的代码传 ...
- springboot的热部署和dubug
采用了项目聚合,产生一些不同,遇到的问题和解决方法分享下. 项目结构: rebuilder2 -htran 主项目 -htran-api 1.htran.pom <parent> < ...
- SSIS 事件的向上传递
在SSIS中,Package是Task组件的有序组合,具有层次结构,Package处于层次结构的顶层(Root Level),对于父子包结构,父包(Parent Package)通过Execute P ...
- nova状态同步
服务初始化阶段 nova-compute服务启动时调用manager中的host初始化函数 self.manager.init_host() 在host初始化函数中完成如下操作: #初始化libvir ...
- java Script复习总结
一:基础知识 1.JavaScript语言的历史 l 早期名称:livescript l 开发公司:网景公司(netscape) 2.JavaScript语言的基本特点 l 基于对象 l 事件 ...
- UE4中Timeline的使用
UE4中经常需要一些和时间相联系的功能,例如在一段时间内完成一个动作,播放一段动画,或者只是单纯的延迟函数的执行时间,即调整事件的执行顺序.在UE4的蓝图自带函数中有一个很好用的函数可以完美地解决这些 ...
- 微软职位内部推荐-Senior Program Manager
微软近期Open的职位: Title: Senior Program Manager – Bing Multimedia Relevance Group: Search Technology Cent ...
- PAT甲题题解-1124. Raffle for Weibo Followers-模拟,水题
水题一个,贴个代码吧. #include <iostream> #include <cstdio> #include <algorithm> #include &l ...
- Daily Scrum - 12/03
Meeting Minutes 后端基本完成,结束当前Sprint, 开始下一个Sprint.进一步讨论了UI,并完成了任务分配. Burndown Progress part 组 ...