[LeetCode] 113. Path Sum II 路径和 II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.
For example:
Given the below binary tree and sum = 22
,
5
/ \
4 8
/ / \
11 13 4
/ \ / \
7 2 5 1
return
[
[5,4,11,2],
[5,8,4,5]
]
112. Path Sum 的拓展,上一题只要求返回是否存在,这题要求输出具体的路径。
Java:
/**
* 和原来的不一样,这题要完全遍历,使用track跟踪当前的进度,进入dfs时压进去,出dfs时推出,当时叶节点且和正好为0是,克隆添加到结果中。
* */
public class Solution {
List<List<Integer>> list;
LinkedList<Integer> track;
public void dfs(TreeNode root,int sum){
if(root==null)
return;
sum-=root.val;
track.add(root.val);
if(sum==0 && root.left==null && root.right==null)
list.add((LinkedList<Integer>)track.clone());
dfs(root.left,sum);
dfs(root.right,sum);
track.remove(track.size()-1); }
public List<List<Integer>> pathSum(TreeNode root, int sum) {
this.list=new ArrayList<List<Integer>>();
this.track=new LinkedList<Integer>();
dfs(root,sum);
return this.list;
}
}
Python:
class Solution:
# @param root, a tree node
# @param sum, an integer
# @return a list of lists of integers
def pathSum(self, root, sum):
return self.pathSumRecu([], [], root, sum) def pathSumRecu(self, result, cur, root, sum):
if root is None:
return result if root.left is None and root.right is None and root.val == sum:
result.append(cur + [root.val])
return result cur.append(root.val)
self.pathSumRecu(result, cur, root.left, sum - root.val)
self.pathSumRecu(result, cur,root.right, sum - root.val)
cur.pop()
return result
C++:
class Solution {
public:
vector<vector<int> > pathSum(TreeNode *root, int sum) {
vector<vector<int>> res;
vector<int> out;
helper(root, sum, out, res);
return res;
}
void helper(TreeNode* node, int sum, vector<int>& out, vector<vector<int>>& res) {
if (!node) return;
out.push_back(node->val);
if (sum == node->val && !node->left && !node->right) {
res.push_back(out);
}
helper(node->left, sum - node->val, out, res);
helper(node->right, sum - node->val, out, res);
out.pop_back();
}
};
类似题目:
[LeetCode] 437. Path Sum III 路径和 III
All LeetCode Questions List 题目汇总
[LeetCode] 113. Path Sum II 路径和 II的更多相关文章
- [LeetCode] 437. Path Sum III 路径和 III
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)
LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...
- [LeetCode] 113. 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. Path Sum II路径总和 II (C++)
题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...
- leetcode 113. 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. Path Sum II (Medium)
原题链接 子母题 112 Path Sum 跟112多了一点就是保存路径 依然用dfs,多了两个vector保存路径 Runtime: 16 ms, faster than 16.09% of C++ ...
- 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance
引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...
- leetcode 437 Path Sum III 路径和
相关问题:112 path sum /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNo ...
- [leetcode]113. 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 ...
随机推荐
- Flooded! UVA - 815 (sort排序)
错了好多遍,不知道为啥出错,如果有大神发现,请求指点!!! 附错误代码(错的不知道怎么回事): #include<iostream> #include<cstdio> #inc ...
- 小白式Git使用教程,从0到1
Git是什么? Git是目前世界上最先进的分布式版本控制系统.工作原理 / 流程: Workspace:工作区 Index / Stage:暂存区 Repository:仓库区(或本地仓库) Remo ...
- 我感觉这个书上的微信小程序登陆写得不好
基本功能是OK,但是感觉传的数据太多,不安全,需要改写. App({ d: { hostUrl: 'http://www.test.com/index.php', //请填写您自己的小程序主机URL ...
- Codeforces_Round_547 (Div. 3)题解
题目链接 传送门 A题 题目 题意 给你两个正整数\(n\)和\(m\),然后你可以进行无数次操作(每次操作可以将\(n\)扩大两倍,或者扩大三倍),问你是否能够得到\(m\). 代码实现如下 n, ...
- CentOS7卸载 OpenJDK 安装Sun的JDK8
Linux上一般会安装Open JDK,关于OpenJDK和JDK的区别:http://www.cnblogs.com/sxdcgaq8080/p/7487369.html 下面开始安装步骤: --- ...
- ARTS-week5
Algorithm 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组.说明:初始化 nums1 和 nums2 的元素数量分别 ...
- Triton 学习
介绍 Triton 是一款动态二进制分析框架,它支持符号执行和污点分析,同时提供了 pintools 的 python 接口,我们可以使用 python 来使用 pintools 的功能. Trito ...
- Spring boot jpa 设定MySQL数据库的自增ID主键值
内容简介 本文主要介绍在使用jpa向数据库添加数据时,如果表中主键为自增ID,对应实体类的设定方法. 实现步骤 只需要在自增主键上添加@GeneratedValue注解就可以实现自增,如下图: 关键代 ...
- oracle 游标使用详解
-- 声明游标:CURSOR cursor_name IS select_statement--For 循环游标--(1)定义游标--(2)定义游标变量--(3)使用for循环来使用这个游标decla ...
- 003-转载-keil-STM32硬件错误HardFault_Handler的处理方法
(一)参考文献:https://blog.csdn.net/electrocrazy/article/details/78173558 在用Keil对STM32的程序进行仿真时程序有时会跑飞,停止仿真 ...