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 ...
随机推荐
- java加载类的方法1.classloader 2.class.forName()
java加载类的方法1.classloader 2.class.forName() 加载一个类后,是在方法去创建这个类的元信息class对象,在方法区立刻创建.在方法区创建.
- [Cocoa]深入浅出Cocoa多线程编程之 block 与 dispatch quene
深入浅出 Cocoa 多线程编程之 block 与 dispatch quene 罗朝辉(http://www.cppblog.com/kesalin CC 许可,转载请注明出处 block 是 Ap ...
- mac安装apache的mod_wsgi模块
第一次用pip安装 ,最终不能使用,原因是系统自带的apache,python和新安装的冲突, 所以需要安装时需要指定apache,python路径 所以用make makeinstall方式 参考链 ...
- leetCode 58.Length of Last Word (最后单词的长度) 解题思路和方法
Length of Last Word Given a string s consists of upper/lower-case alphabets and empty space charact ...
- linux远程登录工具
ssh协议原理
- HTML5 Canvas 绘制英国国旗
代码: <!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type ...
- js 输入框增加删除操作
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 超出用省略号function()
//判断是否微信浏览器 function isWeiXin() { var ua = window.navigator.userAgent.toLowerCase(); if (ua.match(/M ...
- Java 9 模块解耦的设计策略
1. 概述 Java 平台模块系统 (Java Platform Module System,JPMS)提供了更强的封装.更可靠且更好的关注点分离. 但所有的这些方便的功能都需要付出代价.由于模块化的 ...
- Dell 刀片服务器CentOS6.5mini开机20~30分钟宕机
今天查看系统日志发现大量的nf_conntrack: table full, dropping packet. 错误 cat /var/log/messages | moreJun 7 09:52: ...