Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.

Note: A leaf is a node with no children.

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]
]

题意:

二叉树之和,返回所有和等于给定值的路径

思路:

要穷举所有路径。 backtracking。

思路类似two sum, 通过(sum - 当前值) 来check 叶子节点的值是否与一路作减法的sum值相等

代码:

 class Solution {
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> result = new ArrayList<>();
ArrayList<Integer> cur = new ArrayList<>(); // 中间结果
helper(root, sum, cur, result);
return result;
} private static void helper(TreeNode root, int sum, ArrayList<Integer> cur,
List<List<Integer>> result) {
if (root == null) return; cur.add(root.val); // leaf node
if (root.left == null && root.right == null) {
if (sum == root.val)
result.add(new ArrayList<>(cur));
} helper(root.left, sum - root.val, cur, result);
helper(root.right, sum - root.val, cur, result); cur.remove(cur.size() - 1);
}
}

[leetcode]113. Path Sum II路径和(返回路径)的更多相关文章

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

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

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

  4. leetcode 113 path Sum II 路径和

    递归先序遍历+vector<int>容器记录路径 /** * Definition for a binary tree node. * struct TreeNode { * int va ...

  5. [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)

    LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...

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

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

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

  8. [leetcode] 113. Path Sum II (Medium)

    原题链接 子母题 112 Path Sum 跟112多了一点就是保存路径 依然用dfs,多了两个vector保存路径 Runtime: 16 ms, faster than 16.09% of C++ ...

  9. 113 Path Sum II 路径总和 II

    给定一个二叉树和一个和,找到所有从根到叶路径总和等于给定总和的路径.例如,给定下面的二叉树和 sum = 22,              5             / \            4 ...

随机推荐

  1. Vcenter5.5+vmwarePowercli6.5+powershell5批量创建虚拟机

    另存为xxx.ps1 ##########################通过模版批量部署虚拟机以下内容需要人工定义变量####################### #Vcenter的IP $vce ...

  2. [UE4]使机器人受伤

  3. 操他妈的,jquery1.4以上不能用toggle()轮流切换函数

    query 1.9里面已经删除了toggle(fn1, fn2)函数 (2013-05-07 13:44:27) 转载▼ 标签: it 分类: js jquery 1.9里面已经删除了toggle(f ...

  4. jpgraph中文使用手册之文本和字体控制教程

    摘要:在之前的php jpgraph安装配置教程中已介绍过jpgraph字体的安装与配置方法,jpgraph类库中字体和文本的使用是非常重要的,jpgraph既可以控 制文本的旋转.对齐方式.字体大小 ...

  5. Ubuntu下Code::Blocks错误

    #error This file requires compiler and library support for the ISO C++ 2011 standard. This support i ...

  6. 信息学奥赛(NOIP)复赛学习方法推荐

    一.确定你的语言 NOIP包括三种语言c/c++/pascal,在最初必须确定自己使用的语言.没有c/c++基础的,个人建议使用pascal,因为它更容易上手,如果有充裕的时间,则建议c/c++,因为 ...

  7. 基于Linux的Samba开源共享解决方案测试(二)

    单NAS网关50Mb码率视音频文件的稳定读测试结果如下: 50Mb/s负载性能记录 NAS网关资源占用 稳定读 稳定读 CPU空闲 内存空闲 网卡占用 13个稳定流 96.70% 10G 104MB/ ...

  8. Django Middleware 之 SessionMiddleware

    Django版本:1.7.11 先放源码: class SessionMiddleware(object): def __init__(self): engine = import_module(se ...

  9. 折腾了几个小时,分享下zendstudio10的git使用

    今天打开zend10,发现新建项目的地方有 from git,from github,就试了试,发现可以导出,也可以commit,但是没办法push. 就百度百度,发现zendstudio10的git ...

  10. 学习笔记:Zepto笔记

    1.Zepto对象不能自定义事件 例如执行:$({}).bind('cust',function(){}); 结果:TypeError:Object#hasnomethod'addEventListe ...