给定一个二叉树和一个和,找到所有从根到叶路径总和等于给定总和的路径。
例如,
给定下面的二叉树和 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的更多相关文章

  1. 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 ...

  2. 【LeetCode】113. Path Sum II 路径总和 II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 文章目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https:// ...

  3. 437 Path Sum III 路径总和 III

    给定一个二叉树,二叉树的每个节点含有一个整数.找出路径和等于给定数的路径总数.路径不需要从根节点开始,也不需要在叶节点结束,当路径方向必须是向下的(只从父节点到子节点).二叉树不超过1000个节点,节 ...

  4. [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 ...

  5. LeetCode:路径总和II【113】

    LeetCode:路径总和II[113] 题目描述 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例:给定如下二叉树, ...

  6. Java实现 LeetCode 113 路径总和 II

    113. 路径总和 II 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及目标和 sum = ...

  7. 刷题-力扣-113. 路径总和 II

    113. 路径总和 II 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/path-sum-ii 著作权归领扣网络所有.商业转载请联系 ...

  8. 【LeetCode】113. Path Sum II 解题报告(Python)

    [LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...

  9. 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 ...

随机推荐

  1. OSI和TCP/IP

    OSI和TCP/IP 1.        OSI的七层网络结构(功能及特点) 1)  物理层:为数据链路层提供物理连接,在其上串行传送比特流,即所传送数据的单位是比特.此外,该层中还具有确定连接设备的 ...

  2. 从数据的角度带你深入了解IPFS

    IPFS 和区块链有着非常紧密的联系, 随着区块链的不断发展,对数据的存储需求也越来越高.本文从IPFS 的底层设计出发, 结合源代码, 分析了IPFS 的一些技术细节. 一.概述 IPFS 和区块链 ...

  3. Jmeter参数化_CSV Data Set Config

    1. 在用函数助手进行参数化的时候遇到一个问题,每个线程组每次循环的时候读取的值都是一样的,为了解决这个问题,将函数助手替换为CSV_Data_Set_Config. 2. 添加配置元件csv dat ...

  4. 用php描述二分查找法

    //二分查找 $arr = array(0,1,2,3,4,5,6,7,8,9); function bin_sch($array, $low, $high, $k){ if ($low <= ...

  5. 避免复杂的layout

    layout是浏览器计算元素的几何信息:元素在页面上的的大小和位置. 每个元素都有明确的亦或含蓄的大小信息,这些信息基于我们使用的css以及元素的内容被高和父亲元素. 这个过程在 Chrome, Op ...

  6. Spring MVC 注解json 配置

    1.首先在pom.xml中添加依赖jar包 <dependency>        <groupId>org.codehaus.jackson</groupId>  ...

  7. codeforces 665B B. Shopping(水题)

    题目链接: B. Shopping time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  8. 机器学习 : 高斯混合模型及EM算法

    Mixtures of Gaussian 这一讲,我们讨论利用EM (Expectation-Maximization)做概率密度的估计.假设我们有一组训练样本x(1),x(2),...x(m),因为 ...

  9. python 基础之第四天

    例子1: 打印列表每个元素对应的索引 [root@master script]# vim suoyin.py #!/usr/bin/python # coding:utf-8 alist = ['fu ...

  10. [SHOI 2017] 寿司餐厅

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4873 [算法] 注意到题目中的限制条件可表述为 : 若选择区间[L , R] , 则 ...