LeetCode 113. 路径总和 II(Path Sum II)
题目描述
给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径。
说明: 叶子节点是指没有子节点的节点。
示例:
给定如下二叉树,以及目标和 sum = 22,
5
/ \
4 8
/ / \
11 13 4
/ \ / \
7 2 5 1
返回:
[
[5,4,11,2],
[5,8,4,5]
]
解题思路
从树根开始遍历,记录当前路径和,递归向下访问子节点并添加到路径中,若碰到叶节点,判断当前路径和是否等于目标值,若等于就把当前路径加入到结果集中。
代码
/**
* 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:
vector<vector<int>> pathSum(TreeNode* root, int sum) {
vector<vector<int>> res;
if(root == NULL) return res;
vector<int> temp;
findPath(root, sum, , temp, res);
return res;
}
void findPath(TreeNode* root, int sum, int pathSum, vector<int> temp, vector<vector<int>> &res){
if(root->left == NULL && root->right == NULL && root->val + pathSum == sum){
temp.push_back(root->val);
res.push_back(temp);
return;
}
temp.push_back(root->val);
if(root->left) findPath(root->left, sum, pathSum + root->val, temp, res);
if(root->right) findPath(root->right, sum, pathSum + root->val, temp, res);
}
};
LeetCode 113. 路径总和 II(Path Sum II)的更多相关文章
- LeetCode 112. 路径总和(Path Sum)
题目描述 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及目标和 sum ...
- Java实现 LeetCode 113 路径总和 II
113. 路径总和 II 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及目标和 sum = ...
- [Swift]LeetCode113. 路径总和 II | Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- [LeetCode] 113. 路径总和 II
题目链接 : https://leetcode-cn.com/problems/path-sum-ii/ 题目描述: 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径 ...
- LeetCode 113. 路径总和 II C++
提交结果:内存超100%,用时超69% /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNo ...
- 【Leetcode】【Medium】Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- 【leetcode】1289. Minimum Falling Path Sum II
题目如下: Given a square grid of integers arr, a falling path with non-zero shifts is a choice of exactl ...
- [Swift]LeetCode437. 路径总和 III | Path Sum III
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- LeetCode 112. 路径总和(Path Sum) 10
112. 路径总和 112. Path Sum 题目描述 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节 ...
随机推荐
- Python中文分词组件 jieba
jieba "结巴"中文分词:做最好的Python中文分词组件 "Jieba" Feature 支持三种分词模式: 精确模式,试图将句子最精确地切开,适合文本分 ...
- 日常开发用Windows 好还是 Ubuntu好?
最近打算给电脑重新装系统,纠结了很久,不知道应该是换Windows还是Ubuntu,今天通过我自身的体验,来为大家分析一下,日常开发环境到底是用Windows和Ubuntu. [系统介绍] Windo ...
- 使用Django开发简单接口:文章增删改查
目录 1.一些准备工作 安装django 创建django项目 创建博客应用(app) 2.models.py 3.django admin 登录 创建超级用户 4.修改urls.py 5.新增文章接 ...
- MySQL查询多行重复数据SQL
1 详见如下 SELECT day_time,`city_code`,count(1) as num FROM t_user_register_analyse GROUP BY `day_time`, ...
- C++——子类调用父类方法
原创声明:本文系博主原创文章,转载或引用请注明出处. 1. 如果类B是类A的子类,则在类B的成员方法中调用类A的方法时,可以直接以 A::method(paramlist); 来调用. 2. 若子类B ...
- 【idea】idea 2018.2 for mac永久破解激活方法(亲测2099)
1. 下载安装idea: 2. 下载激活Jar包 链接:https://pan.baidu.com/s/1NaxYrDNi2eW66epjmk10dg 密码:aec5 3. 在访达中新建/Librar ...
- BZOJ1491 [NOI2007]社交网络[最短路计数]
$n$非常的小,结合题目计算式可以想到$O(n^3)$暴枚$s,t,v$,看$v$在不在$s\to t$最短路上($dis_{s,v}+dis_{v,t}=dis_{s,v}$是$v$在两点最短路上的 ...
- ios 打包下
一.打包真机方式 二.编译打包 三.配置打包信息 以下为打的包:
- elementUI + vue 输入框只能输入正整数 不能输入字母 e 以及+ - 号
<el-input :inline="true" v-model="dialogForm.closeTime" onKeypress="retu ...
- 一例tornado框架下处理上传图片并生成缩略图的例子
class coachpic(RequestHandler): @gen.coroutine def post(self): picurl = self.request.files[] print(& ...