由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - question about Leetcode #113 LeetCode – Path Sum II (Java)
相关主题
又有leetcode题目来请教了leetcode的3sum的运行时间问题
path sum II OJ 超时请教leetcode Subsets II
请教一道google面试题过不了leetcode Zigzag Level Order Traversal
F电面T家 :: 面筋
问道leetcode的题:Combination Sum IIleetcode的OJ也会有错吗??
问一道leetcode上的题目 combination sumTree的traversal也分BFS和DFS?
a problem from leetcode: high efficiency algorithm for combinations problemBinary Tree Level Order Traversal为什么老通不过
我来问个面经:打印binary tree 从root到leaf的所有pathinorder traversal的空间复杂度是O(N) 还是O(logN)?
相关话题的讨论汇总
话题: list话题: integer话题: sum话题: arraylist
进入JobHunting版参与讨论
1 (共1页)
y*******d
发帖数: 1674
1
just started leetcode practice.
has a question about #113:
public class Solution {
public List> pathSum(TreeNode root, int sum) {
List> all = new ArrayList();
findPathSum(root, sum, all, new ArrayList());
return all;
}

private void findPathSum(TreeNode node, int sum, List>
result, List l) {
if (node == null) {
return;
}
l.add(node.val);
if (node.left == null && node.right == null && node.val == sum) {
// has to make a copy, otherwise the content may be changed
ArrayList curPath = new ArrayList(l);
result.add(curPath);
}
findPathSum(node.left, sum - node.val, result, l);
findPathSum(node.right, sum - node.val, result, l);
l.remove(l.size() - 1);
}
}
why is l.remove(l.size() - 1);?
c********t
发帖数: 5706
2
backtracking, it removes node value from list

【在 y*******d 的大作中提到】
: just started leetcode practice.
: has a question about #113:
: public class Solution {
: public List> pathSum(TreeNode root, int sum) {
: List> all = new ArrayList();
: findPathSum(root, sum, all, new ArrayList());
: return all;
: }
:
: private void findPathSum(TreeNode node, int sum, List>

s**********g
发帖数: 14942
3
backtracking一般都要repair
不然下一个尝试怎么做
这个就是repair

【在 y*******d 的大作中提到】
: just started leetcode practice.
: has a question about #113:
: public class Solution {
: public List> pathSum(TreeNode root, int sum) {
: List> all = new ArrayList();
: findPathSum(root, sum, all, new ArrayList());
: return all;
: }
:
: private void findPathSum(TreeNode node, int sum, List>

1 (共1页)
进入JobHunting版参与讨论
相关主题
inorder traversal的空间复杂度是O(N) 还是O(logN)?问道leetcode的题:Combination Sum II
问个fb onsite题目问一道leetcode上的题目 combination sum
请问一道关于binary tree的题a problem from leetcode: high efficiency algorithm for combinations problem
我又fail了面试我来问个面经:打印binary tree 从root到leaf的所有path
又有leetcode题目来请教了leetcode的3sum的运行时间问题
path sum II OJ 超时请教leetcode Subsets II
请教一道google面试题过不了leetcode Zigzag Level Order Traversal
F电面T家 :: 面筋
相关话题的讨论汇总
话题: list话题: integer话题: sum话题: arraylist