GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode/code/lc113.java at master · mJackie/leetcode

Expand file tree

/

lc113.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
39 lines (38 loc) · 1.25 KB
/

lc113.java

Copy path

File metadata and controls

39 lines (38 loc) · 1.25 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package code;
import java.util.ArrayList;
import java.util.List;
/*
* 113. Path Sum II
* 题意:找从root到叶子节点和为sum的路径
* 难度:Medium
* 分类:Tree, Depth-first Search
* 思路:回溯,注意因为节点上可能正值,可能负值,所以不能剪枝
* Tips:lc112, lc113, lc437, lc129, lc124, lc337
*/
public class lc113 {
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> res = new ArrayList();
helper(res, new ArrayList(), root, sum);
return res;
}
public void helper(List<List<Integer>> res, List<Integer> cur, TreeNode root, int sum){
if(root==null) return;
cur.add(root.val);
if(root.left==null&&root.right==null&&root.val==sum){ //到叶子节点
res.add(new ArrayList(cur));
}else{
helper(res, cur, root.left, sum-root.val);
helper(res, cur, root.right, sum-root.val);
}
cur.remove(cur.size()-1); //注意是去掉最后一个,传的是索引。传递对象的话,序列可能会变。
return;
}
}
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.