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 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的更多相关文章
- [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)
LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...
- [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 ...
- [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 ...
- 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 ...
- [leetcode] 113. Path Sum II (Medium)
原题链接 子母题 112 Path Sum 跟112多了一点就是保存路径 依然用dfs,多了两个vector保存路径 Runtime: 16 ms, faster than 16.09% of C++ ...
- 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 ...
- 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 ...
- 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 ...
- [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 ...
随机推荐
- 使用Google API 下拉刷新或者增加数据 SwipeRefreshLayout
贴出布局代码: <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/id_swipe_ly" and ...
- BinaryWriter
c#里的文件操作 fileInfo dir的一大堆属性不用说 地球人都知道(什么fileName,create() delete()) ,文件系统的概念很好理解的 文件读写也好理解(硬盘到内存 然后再 ...
- iOS开发之单例设计模式(完整正确版本)
单例的意思从字面上就可以略知一二,所谓单例就是确保在程序运行过程中只创建一个对象实例.可以用于需要被多次广泛或者说多次使用的资源中,比如我们常见的网络请求类.工具类以及其它管理类等.比如我iOS开发中 ...
- Mahout0.9的安装与测试
最近想实协同过滤的MR算法,但是网上查了一下,发现hadoop的生态系统中的Mahout的项目已经实现了相应的算法,因此想先尝试着实时这个mahout的使用及效果.要想用mahout必须要部署到had ...
- for循环进阶
[引例] 输出一行10个“*” #include<cstdio> int main(){ printf("**********\n"); ; } 思考: (1)输出一行 ...
- c规范(1)
1文件结构 头文件.h 保存文件声明 定义文件.c 程序实现 2版本标示 用注释 (1)版权信息. (2)文件名称,标识符,摘要. (3)当前版本号,作者 修改者,完成日期. (4)版本历史信息. ...
- Mysql5.0以上 手工注入
Mysql5.0以上 order by 23 http://www..com/productdet.php?&id=89 and 1=2 UNION SELECT 1,2,3,4,5,6,7, ...
- sql 如何把查询得到的结果如何放入一个新表中
如何把这个查询到的结果放到一张新表中? 2014-03-13 15:26 提问者采纳 表已经存在:insert into 表名 (列名1... 列名n) select 列名1....列名n f ...
- 多数求和(java)
实验题目:从命令行接受多个数字,求和之后输出结果. 设计思想:命令行输入的字符会赋值给args数组,所以在命令行输入数字后,直接取出args的数组长度,作为循环语句的终点判断,然后利用循环将字符型改为 ...
- python 优雅的使用正则表达式 ~ 2
使用正则表达式 那些基础的理论也说了不少了现在就开始 实操 ( 不知道为啥特别喜欢这个词... ) 吧 . 上一节课说过 正则表达式也是一门语言 , 他被集成到了python当中 , 并且用 re 模 ...