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

解题思路:

DFS,JAVA实现如下:

	static public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> list = new ArrayList<List<Integer>>();
List<Integer> alist = new ArrayList<Integer>();
if (root != null)
dfs(alist,list, root, sum);
return list;
} public static void dfs(List<Integer> alist,List<List<Integer>> list, TreeNode root, int sum) {
if (root.left == null && root.right == null) {
if (sum == root.val) {
alist.add(root.val);
list.add(new ArrayList<Integer>(alist));
alist.remove(alist.size() - 1);
}
return;
}
alist.add(root.val);
if (root.left == null)
dfs(alist,list, root.right, sum - root.val);
else if (root.right == null)
dfs(alist,list, root.left, sum - root.val);
else {
List<Integer> alistCopy = new ArrayList<Integer>(alist);
dfs(alist,list, root.right, sum - root.val);
alist=alistCopy;
dfs(alist,list, root.left, sum - root.val);
} }

Java for LeetCode 113 Path Sum II的更多相关文章

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

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

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

  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 (路径和) 解题思路和方法

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

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

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

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

  7. leetcode 113 Path Sum II ----- java

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

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

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

随机推荐

  1. Android Retrofit使用教程(三):Retrofit与RxJava初相逢

    上一篇文章讲述了Retrofit的基本使用,包括GET,POST等请求.今天的文章中Retrofit要与RxJava配合使用. 了解RxJava RxJava有种种好处,我不在这里一一讲述.这里我只给 ...

  2. lua 的一些常用概念

    1 a={} //定义了一个table   a a[10000]=1 //这里的table中只有一个元素,10000,而不是有10000个元素 2 x=math.pi //定义了x等于π print( ...

  3. TP多条件查询

    $stcount = M("Results_all")->alias('a') ->join("s_test_name as b on a.subject = ...

  4. Linux中运行c程序,与系统打交道

    例一:system系统调用是为了方便调用外部程序,执行完毕后返回调用进程. #include <stdio.h> #include <stdlib.h> main() { pr ...

  5. 学习使用用Eclipse编写java程序

    本文讲解了在Eclipse中完成一个HelloWorld程序的编写过程. 刚刚学习java的同学们可能用 记事本编写java源代码,在命令提示符中完成java程序的编译和运行过程.这样的方法对于学习j ...

  6. win7 iis6怎么部署.net网站

    win7 iis6怎么部署.net网站,把本机当成网站服务器来简单介绍. 方法/步骤 1 首先在本机有一个可以正常运行的网站.比如vs2010中有一个网站项目,网站项目运行后正常. 2 打开iis6, ...

  7. Java函数的基本知识

    http://blog.csdn.net/cxwen78/article/details/7322891主要从Java函数的定义,函数的特点,函数的应用,函数的重载四个方面来讲解Java函数. 一.函 ...

  8. 点击出现黑色背景的解决:-webkit-tap-highlight-color:rgba(0,0,0,0)

    在手机上(iphone)点击按钮的时候,屏幕总会闪动一下,这让页面看起来很不友好也不流畅.解决方案加了一句css就解决了: -webkit-tap-highlight-color:rgba(0,0,0 ...

  9. 【Android】getActionBar()为null的解决方法总结

    前言 在使用 ActionBar的时候,有时候会爆出空指针异常,这是由于应用没有获取到 ActionBar 导致的,而导致应用没有获取到 ActionBar 的原因比較多.所以我们以下就来总结一下 A ...

  10. Android - 单例模式(singleton)的使用

    单例模式(singleton)的使用 本文地址:http://blog.csdn.net/caroline_wendy 单例(singleton)是特殊的Java类,在创建实例时.一个类仅同意创建一个 ...