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

上一道题的延伸,求出所有结果。

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
List list = new ArrayList<List<Integer>>();
public List<List<Integer>> pathSum(TreeNode root, int sum) { if( root == null)
return list;
getResult(root,sum,new ArrayList<Integer>(),0);
return list;
} public void getResult(TreeNode root,int sum,List<Integer> ans,int pos){ if( root == null )
return ;
ans.add(root.val);
if( sum == root.val ){
if( root.left == null && root.right == null){
ArrayList result = new ArrayList<Integer>();
result.addAll(ans);
list.add(result);
}
}
getResult(root.left,sum-root.val,ans,pos+1);
getResult(root.right,sum-root.val,ans,pos+1);
ans.remove(pos);
return ; }
}

leetcode 113 Path Sum II ----- java的更多相关文章

  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. Java for 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 ...

  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. C#操作Access数据库(创建&修改结构)

    本文转自:http://www.cnblogs.com/liyugang/archive/2012/11/17/2775393.html 想要在程序中控制Access,不是数据,而是Access数据库 ...

  2. High Performance Django

    构建高性能Django站点   性能 可用 伸缩 扩展 安全 build 1.审慎引入第三方库(是否活跃.是否带入query.是否容易缓存) 2.db:减少query次数 减少耗时query 减小返回 ...

  3. ajax异步文件上传,iframe方式

    不是我写的,我看了他的,思路很明确: 实现思路: 在js脚本中动态创建form,动态创建form中的内容,将文件上传的内容以隐藏域的方式提交过去,然后写好回调等. 感觉思路不难,但是我写不出来,感觉需 ...

  4. springmvc----struts2比较

    method=requestMethod.GETorPOST  vs  addInput+add 用抛异常处理密码验证和用户名重复与否验证 +js303 @validate   判断输入格式(jque ...

  5. Codeforces Round #230 (Div. 2) 解题报告

    Problem A. Nineteen 思路: 除了首位像连的n,其他的字母不能共用nineteenineteen.所以可以扫描一遍所有的字符串将出现次数保存到hash数组,n的次数(n - 1) / ...

  6. Eclipse卡死问题解决办法

    偶尔浏览到几个eclipse卡死的文章,收集一下. 1.  eclipse 3.6卡死 eclipse自动提示反应慢,或者卡死, 有人说这是eclipse 3.6的版本bug, 但是3.5版本好像也有 ...

  7. Linux Mint下安装JDK

    Linux Mint 17下安装的是默认的OpenJDK,可以使用java -version查看 现在需要使用Sun/Oracle官方的JDK:http://www.oracle.com/techne ...

  8. 【Leetcode】 LRU Cache实现

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

  9. 横竖屏切换时候Activity的生命周期

    1.新建一个Activity,并把各个生命周期打印出来 2.运行Activity,得到如下信息 onCreate-->onStart-->onResume--> 3.按crtl+f1 ...

  10. IOS 作业项目(4)步步完成 画图 程序(剧终)

    // //  CHViewController.m //  SuperDrawingSample // //  Created by JaikenLI on 13-11-21. //  Copyrig ...