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

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

给定一个二叉树和一个值。找出全部根到叶的路径和等于这个值的路径。

深度优先遍历。

	public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> ret = new ArrayList<List<Integer>>();
List<Integer> list = new ArrayList<Integer>();
dfs(root,sum,ret,list);
return ret;
}
public void dfs(TreeNode root,int sum,List<List<Integer>> ret,List<Integer> list){
if(root == null)
return ;
if(root.val == sum && root.left == null && root.right == null){
list.add(root.val);
List<Integer> temp = new ArrayList<Integer>(list);//拷贝一份
ret.add(temp);
list.remove(list.size() - 1);//再删除
return ;
}
list.add(root.val);
dfs(root.left,sum-root.val,ret,list);
dfs(root.right,sum-root.val,ret,list);
list.remove(list.size() - 1);
}
// Definition for binary tree
public class TreeNode {
int val;
TreeNode left;
TreeNode right; TreeNode(int x) {
val = x;
}
}

LeetCode——Path Sum II的更多相关文章

  1. [leetcode]Path Sum II

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

  2. LeetCode: Path Sum II 解题报告

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

  3. [LeetCode] 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] 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 ...

  5. [leetcode]Path Sum II @ Python

    原题地址:https://oj.leetcode.com/problems/path-sum-ii/ 题意: Given a binary tree and a sum, find all root- ...

  6. leetcode: 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] 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 ...

  8. LeetCode Path Sum II (DFS)

    题意: 给一棵二叉树,每个叶子到根的路径之和为sum的,将所有可能的路径装进vector返回. 思路: 节点的值可能为负的.这样子就必须到了叶节点才能判断,而不能中途进行剪枝. /** * Defin ...

  9. LeetCode:Path Sum I II

    LeetCode:Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such ...

随机推荐

  1. Jquery 遍历数组之grep()方法介绍

    grep()方法用于数组元素过滤筛选. grep(array,callback,boolean);方法参数介绍. array   ---待处理数组 callback  ---这个回调函数用来处理数组中 ...

  2. Cookie获取、设置值

    设置: HttpCookie cookie = new HttpCookie("cookieName"); cookie.Value = "name1" Htt ...

  3. win7程序关闭后弹出 程序兼容性助手 这个程序可能安装不正确 如果此程序没有正确安装,处理方式

    用WTL编写的程序,编译成release后,在win7上关闭后,弹出这个对话框 处理方法: 将这个xml文件命名成 xxx.exe.manifest,保存到指定目录下 <?xml version ...

  4. BZOJ 1997: [Hnoi2010]Planar( 2sat )

    平面图中E ≤ V*2-6.. 一个圈上2个点的边可以是在外或者内, 经典的2sat问题.. ----------------------------------------------------- ...

  5. C++对象模型2--指针cout结果

    在开始之前,首先科普一下cout指针的知识,这样才能在测试程序中很好的理解: 看下面的代码: void main(void) { int a = 10; int *p = &a; cout & ...

  6. Java学习之异常处理

    在 Java 中,所有的异常都有一个共同的祖先 Throwable(可抛出),Throwable 指定代码中可用异常传播机制通过 Java 应用程序传输的任何问题的共性.       Throwabl ...

  7. poj3358 Period of an Infinite Binary Expansion 数论有难度

    这道题目感觉好难,根本就是无从下手的感觉,尝试了以前的所有方法,都没有思路,毫无进展,参考了一下别人的思路,感觉学到了新的知识 接下来开始分析 观察1/10这组数据,按照二进制转化法可以得到: 1/1 ...

  8. 两台linux机器文件传输之scp

    0.写在前面:一定要注意我们是否有源文件的读权限,是否有目标文件夹的写权限!没有的话要先把权限设置好! *.设置权限的方法:切换到有权限操作文件或文件夹的用户,利用chmod命令修改权限 1.安装: ...

  9. 【Kill】两条Linux命令彻底杀死Oracle

    今天编写的两条极具杀伤力的命令,它可以瞬间将Oracle杀死在无形之中.后面我将给出简单注释并展示一下它的威力.$ ps -ef |grep $ORACLE_SID|grep -v grep|awk ...

  10. installscript类型 完成时实现推荐安装其他产品的功能

    目前好多软件在安装完成时都有什么 立刻运行.打开网址.推荐安装其他工具等功能 我司领导也追时髦要求了这个功能而且要推荐多个,所以这个功能实现起来就需要自己去写代码了.陆陆续续研究了研究了好长时间,由于 ...