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 题目描述 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节 ...
随机推荐
- JavaScript的常用浏览器设置
用什么浏览器?如果您不告诉我您使用的浏览器,我将告诉您有关JavaScript的常用浏览器设置.~火狐在菜单栏中选择工具->选项->内容以查看启用javascript的选项.Interne ...
- 如何使用koa搭建一个简单服务
1.首先检测是否已经有node环境? 把Windows的黑窗体的命令行工具调用出来 敲击命令行node -v , 然后,就可以看到这个打印出了一个版本号,这就证明我们的node.js已经是安装 ...
- Hive的日志操作
想要看hive的日志,我们查看/home/hadoop/hive/conf/hive-log4j2.properties # list of properties property.hive.log. ...
- 卡片拖拽(vue拖拽事件)
<template> <div class="wrapper wrapper-content" id="main" v-cloak> & ...
- 第十章、time模块
目录 第十章.模块 第十章.模块 time模块 import time 时间戳 表示:是从1970年1月1日00:00:00开始按秒计算的偏移量. time_stamp = time.time() p ...
- Python3简易接口自动化测试框架设计与实现(上)
目录 1.开发环境 2.用到的模块 3.框架设计 3.1.流程 3.2.项目结构 5.日志打印 6.接口请求类封装 接口开发请参考:使用Django开发简单接口:文章增删改查 1.开发环境 操作系统: ...
- Asp.Net Zero通用打印实现
Asp.Net Zero是一款非常优秀的web框架,可以用来快速构建业务系统.框架满足了业务系统所需的大部分通用功能,但是系统必须的打印报表功能一直没有实现.下面给大家介绍如何在zero中集成打印功能 ...
- 博弈论BOSS
基础博弈的小结:http://blog.csdn.net/acm_cxlove/article/details/7854530 经典翻硬币游戏小结:http://blog.csdn.net/acm_c ...
- pycharm 怎么能像在命令行中输入参数进行调试
pycharm中配置main参数 Run->Edit Configurations->Script Parames 把需要在xxx.py A B C 后面的参数输入到如下位置. 否则会报错 ...
- 解决使用vue打包时vendor文件过大或者是app.js文件很大的问题
这篇文章主要介绍了使用vue打包时vendor文件过大或者是app.js文件很大问题的解决方法,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下 第一次使用vue2.0开发,之前都是用的angu ...