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 ...
随机推荐
- 20155310 《网络攻防》Exp4 恶意代码分析
20155310 <网络攻防>Exp4 恶意代码分析 基础问题 1.如果在工作中怀疑一台主机上有恶意代码,但只是猜想,所有想监控下系统一天天的到底在干些什么.请设计下你想监控的操作有哪些, ...
- Nginx日常报错处理总结
在Nginx错误日志中,有大量的下列信息: Upstream timed out (110: Connection timed out) while reading response header f ...
- 10、Dockerfile实战-PHP
一.镜像制作步骤 安装编译依赖包 编译安装 配置 二.编写Dockerfile FROM centos:7 MAINTAINER QUNXUE RUN yum install -y gcc gcc-c ...
- effective c++ 笔记 (1-3)
// // effective c++.cpp // 笔记 // // Created by fam on 15/3/23. // // //-------------------------- ...
- 微信小程序初体验与DEMO分享
前言 前一段时间微信公布小程序,瞬间引来了大量的关注.博主的公司也将其定为目标之一,遂派本菜为先头兵(踩坑侠). 这次开发了一个比较完整的DEMO,模仿自某个APP首页,由于保护隐私的目的我把数据拷贝 ...
- Dive查看docker镜像层信息
1.主要采用docker运行dive的方式,不然宿主机还要安装go那些挺麻烦的.具体用法可查看官方: https://github.com/wagoodman/dive 2.拉取dive镜像 dock ...
- mssql循环记录之while方法
1.定义变量 Declare @i Int 2.获取单条记录 Select @i=Min([id]) From [数据库名] Where <检索条件> 3.While循环 While @i ...
- Python数据信号处理库RadioDSP: 引入ThinkDSP实现思想
RadioDSP是针对无线通信领域的数字信号处理库,它采用了ThinkDSP的思想,对于无线通信中的IQ信号可以绘制频谱图和时域图.目前项目还在起始阶段,详细的代码可以参考链接: https://gi ...
- ini_set的用法介绍
https://www.cnblogs.com/xieqian111/p/5367732.html
- 【SE】Week1 : 四则运算题目生成器批改器程序总结
用户需求详见:http://www.cnblogs.com/jiel/p/4810756.html 1)PSP表格分析(预计耗时): PSP2.1 Personal Software Process ...