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 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的更多相关文章
- [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 ...
- 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 ...
- 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 ...
随机推荐
- 适配iOS7之—UITableView和UISearchBar
iOS7中,如果用UITableViewStyleGrouped的话,里面的cell会比原来的拉长了,这样做应该是为了统一和UITableViewStylePlain风格时cell的大小一致,所以改用 ...
- nginx 启动,重启,添加开机启动等相关命令
nginx -t 测试 配置文件是否正确,同时可以查看配置文件路径 nginx -c /usr/local/nginx/conf/nginx.conf 启动nginx ps -ef|grep ng ...
- HDU 5402 Travelling Salesman Problem(多校9 模拟)
题目链接:pid=5402">http://acm.hdu.edu.cn/showproblem.php?pid=5402 题意:给出一个n×m的矩阵,位置(i.j)有一个非负权值. ...
- newlisp HTTP Basic Authentication
HTTP Basic Authentication原来很easy,參考文档:http://zh.wikipedia.org/wiki/HTTP%E5%9F%BA%E6%9C%AC%E8%AE%A4%E ...
- 2017.3.27 集成modeler后的一些主要路径(持续更新)
1.设计器访问路径 项目名:wfs_web edtor-app和modeler.html的存放位置:webapp/designer/editor-app app-cfg.js中根路径设置:'conte ...
- 2017.3.14 activiti实战--第二十章--REST服务
学习资料:<Activiti实战> 第二十章 REST服务 20.1 通信协议概述 略. 20.2 REST API概述 资源分类 资源基础URI 说明 Deployments manag ...
- 解决使用maven jetty启动后无法加载修改过后的静态资源
jetty模式是不能修改js文件的,比如你现在调试前端js,发现在myeclipse/eclipse的源码里面无法修改文件,点都不让你点,所以,你只能采用一些办法,更改jetty的模式配置. Look ...
- java8新特性学习笔记(二) 流的相关思想
流是什么 流是Java API的新成员,他允许你以声明的方式处理数据集合,就现在来说,可以把他们看成遍历数据集合的高级迭代器.此外,流还可以透明地并行处理,你无须写任何多线程代码. 下面例子是新老AP ...
- project修改时间日历
视图→甘特图 格式→时间表→右键时间表 详细的日程表,然后双击时间即可
- 使用struts2完成ckeditor和图片上传
代码地址如下:http://www.demodashi.com/demo/12427.html 使用struts2完成ckeditor和ckeditor图片上传 ckeditor版本ckeditor_ ...