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 题目描述 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节 ...
随机推荐
- java文件上传复制等功能
package com.sitech.message.controller.task;import java.io.File;//引入类 import java.io.FileInputStream; ...
- CSS总结六:动画(一)ransition:过渡、animation:动画、贝塞尔曲线、step值的应用
transition-property transition-duration transition-timing-function transition-delay animation-name a ...
- 用php连接数据库,并执行数据库操作
1,建立与数据库之间的连接 (能通过php代码执行一个SQL语句得到查询的结果) <?php mysqli_connect(' , 'demo01'); 这里要注意两个问题: ①mysqli 是 ...
- 小程序API接口调用
1.在config.js中写入api接口及appkey 2.在HTTP.js中引入config.js,然后新建HTTP.js,在里进行wx.request的封装. 定义一个HTTP的类,来类里定义 ...
- Perl数据类型
Perl 是一种弱类型语言,所以变量不需要指定类型,Perl 解释器会根据上下文自动选择匹配类型. Perl 有三个基本的数据类型:标量.数组.哈希.以下是这三种数据类型的说明: 标量 标量是Perl ...
- JavaJDBC【五、事务】
概念: 事务(Transaction)作为单个逻辑工作单元执行的一系列操作. 这些操作都是作为一个整体一起向系统提交,要么都执行,要么都不执行. 特点: 原子性:一个完整操作. 一致性:当事务完成时, ...
- MacBook Pro实现共享屏幕(多台mac之间)
1 开启允许所有用户控制屏幕 设置— 共享 2 在另一台MacBook Pro里打开finder窗口,并设置好network显示边栏 3 另一台机器发送请求控制 找 ...
- IPC之shm.c源码解读
// SPDX-License-Identifier: GPL-2.0 /* * linux/ipc/shm.c * Copyright (C) 1992, 1993 Krishna Balasubr ...
- CentOS7 xrdp 安装和设置
1) 安装 $ sudo yum install xrdp $ sudo yum install tigervnc $ sudo yum install tigervnc-server 2) 设置密码 ...
- .npy文件怎么打开
import numpy as np test = np.load(r'C:\Users\SAM\PycharmProjects\TEAMWORK\Preprocess_3D\muchdata-50- ...