113 Path Sum II 路径总和 II
给定一个二叉树和一个和,找到所有从根到叶路径总和等于给定总和的路径。
例如,
给定下面的二叉树和 sum = 22,
5
/ \
4 8
/ / \
11 13 4
/ \ / \
7 2 5 1
返回
[
[5,4,11,2],
[5,8,4,5]
]
详见:https://leetcode.com/problems/path-sum-ii/description/
Java实现:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> res=new ArrayList<List<Integer>>();
if(root==null){
return res;
}
helper(root,sum,new ArrayList<Integer>(),res);
return res;
}
private void helper(TreeNode root,int sum,ArrayList<Integer> path,List<List<Integer>> res){
if(root==null){
return;
}
path.add(root.val);
if(root.val==sum&&root.left==null&&root.right==null){
res.add(new ArrayList<Integer>(path));
}else{
helper(root.left,sum-root.val,path,res);
helper(root.right,sum-root.val,path,res);
}
path.remove(path.size()-1);
}
}
python实现:
113 Path Sum II 路径总和 II的更多相关文章
- 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 路径总和 II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 文章目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https:// ...
- 437 Path Sum III 路径总和 III
给定一个二叉树,二叉树的每个节点含有一个整数.找出路径和等于给定数的路径总数.路径不需要从根节点开始,也不需要在叶节点结束,当路径方向必须是向下的(只从父节点到子节点).二叉树不超过1000个节点,节 ...
- [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 su ...
- LeetCode:路径总和II【113】
LeetCode:路径总和II[113] 题目描述 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例:给定如下二叉树, ...
- Java实现 LeetCode 113 路径总和 II
113. 路径总和 II 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及目标和 sum = ...
- 刷题-力扣-113. 路径总和 II
113. 路径总和 II 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/path-sum-ii 著作权归领扣网络所有.商业转载请联系 ...
- 【LeetCode】113. Path Sum II 解题报告(Python)
[LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- leetcode 112. Path Sum 、 113. Path Sum II 、437. Path Sum III
112. Path Sum 自己的一个错误写法: class Solution { public: bool hasPathSum(TreeNode* root, int sum) { if(root ...
随机推荐
- Java 出现“Illegal key size”错误的解决方案
用AES加密时出现"java.security.InvalidKeyException: Illegal key size"异常. 如果密钥大于128, 会抛出上述异常.因为密钥长 ...
- FindBugs规则整理
http://blog.csdn.net/jdsjlzx/article/details/21472253/ 配置FindBugs和常见FindBugs错误 http://blog.csdn.net/ ...
- error MSB6006: “CL.exe”已退出,代码为 -1073741502。
错误1error MSB6006: “CL.exe”已退出,代码为 -1073741502.C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\Plat ...
- Objective-C学习之解析XML
通过soap请求webservice时,返回的数据是XML类型,有时候也需要解析本地的xml数据等,苹果自带类NSXMLParser解析xml还是很方便的,简单轻便 本文以解析本地XML为例,网络获取 ...
- Lucene小例子
package org.itat.test; import java.io.File; import java.io.FileReader; import java.io.IOException; i ...
- Struts2 Action 匹配的几种方式
下面针对我所遇见的Action的配置方法进行一下总结: 1.基本的匹配方法
- java学习之super关键字
对于具有public或者protected属性的父类,其子类若想继承父亲的属性或者方法,那么需要用到super 实例:
- OpenCV——PS滤镜 水波效果
// define head function #ifndef PS_ALGORITHM_H_INCLUDED #define PS_ALGORITHM_H_INCLUDED #include < ...
- (转)使用cygwin注意事项一
原文出处:http://gotgit.readthedocs.io/en/latest/01-meet-git/050-install-on-windows-cygwin.html 在Windows下 ...
- BZOJ_1671_[Usaco2005 Dec]Knights of Ni 骑士_BFS
Description Bessie is in Camelot and has encountered a sticky situation: she needs to pass through t ...