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. [UE4]C++静态局部变量

    void testFunc() { ; // this only runs ONCE, even on // subsequent calls to testFunc()! cout << ...

  2. 与PHP5.3.5的战斗----记php5.3.5安装过程

    与PHP5.3.5的战斗----记php5.3.5安装过程 摘自:http://blog.csdn.net/lgg201/article/details/6125189这篇文章写的很是不错,,,也是我 ...

  3. Immunity Debugger学习笔记

    图1::Immunity主界面 注意事项:最下方的PyCommands窗格既可以执行调试命令也可以执行python脚步文件. 1.PyCommands学习 在 Immunity 中执行 Python ...

  4. DIY-组装

    DIY:-组装 组装,现在基本什么都可以组装,就像计算机,手机,自己进行定制,同样操作系统可以自己组装,软件开发也要组装,现在就是一个DIY的时代. 大家了解DIY,说白了就是自己定制组装一些东西,比 ...

  5. OpenCL + OpenCV 图像旋转

    ▶ 使用 OpenCV 从文件读取彩色的 png 图像,旋转一定角度以后写回文件 ● 代码,核函数 // rotate.cl //__constant sampler_t sampler = CLK_ ...

  6. ASCII字符串互换

    //ASCII码转成字符: var a:String=String.fromCharCode(97); trace(a);//输出:a //字符转成ASCII码: var str:String = “ ...

  7. as3 运算与检查String 是否能够正确转换成数 值

    如果忘了对一个Number 型变量初始化,那么这个变量参与的任何数学运算的结果都是NaN:如果最终结果赋值给有声明类型的变量,那么为该变量的默认值(仅限uint ,int). var a:Number ...

  8. 14 ConfigParse模块

    1.ConfigParse模块的基本概念 此模块用于生成和修改常见配置文档. ConfigParser 是用来读取配置文件的包. 配置文件的格式如下:中括号“[ ]”内包含的为section.sect ...

  9. 8 python time$datetime

    1.表示时间的方式 (1)时间戳 时间戳(timestamp)的方式:通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量. 我们运行“type(time.time())” ...

  10. java并发--流量控制demo

    实现一个流控程序.控制客户端每秒调用某个远程服务不超过N次,客户端是会多线程并发调用,需要一个轻量简洁的实现,大家看看下面的一个实现,然后可以自己写一个实现. import java.util.Dat ...