给定一个二叉树和一个和,找到所有从根到叶路径总和等于给定总和的路径。
例如,
给定下面的二叉树和 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. H264视频通过RTMP直播

    http://blog.csdn.net/firehood_/article/details/8783589 前面的文章中提到了通过RTSP(Real Time Streaming Protocol) ...

  2. C++的逐过程和逐语句的区别

    1.逐语句是指在遇到函数调用语句的时候进入到函数内部执行. 2.逐过程是指在遇到函数调用语句时把函数当作一条语句执行.

  3. static静态数据的初始化

    package com.demo.book; public class StaticInitialization { static Table table = new Table(); static ...

  4. 第二篇:python基础之核心风格

    阅读目录 一.语句和语法 二.变量定义与赋值 三.内存管理 内存管理: 引用计数: 简单例子 四.python对象 五.标识符 六.专用下划线标识符 七.编写模块基本风格 八.示范 一.语句和语法 # ...

  5. tensorflow sigmoid_cross_entropy_with_logits 函数解释

    tf.nn.sigmoid_cross_entropy_with_logits(_sentinel=None,labels=None, logits=None, name=None) sigmoid_ ...

  6. HDU2147 kiki's game (SG表找规律)

    Recently kiki has nothing to do. While she is bored, an idea appears in his mind, she just playes th ...

  7. @Autowired注解和启动自动扫描的三种方式(spring bean配置自动扫描功能的三种方式)

    前言: @Autowired注解代码定义 @Target({ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD, Elemen ...

  8. B - Equidistant String

    B - Equidistant String Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & % ...

  9. ElasticSearch基础之查询功能

    [01]查询类型: [02]基本查询和组合查询是参与打分的 1.创建映射: 注意事项:基于上面映射的创建: "type": "keyword" # 如果某个字段 ...

  10. 20个Flutter实例视频教程-第07节: 毛玻璃效果制作

    视频地址: https://www.bilibili.com/video/av39709290/?p=7 博客地址: https://jspang.com/post/flutterDemo.html# ...