mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 08:50:15 +08:00
@ -377,22 +377,22 @@ class solution {
|
||||
|
||||
```java
|
||||
class solution {
|
||||
public list<list<integer>> pathsum(treenode root, int targetsum) {
|
||||
list<list<integer>> res = new arraylist<>();
|
||||
public List<List<Integer>> pathsum(TreeNode root, int targetsum) {
|
||||
List<List<Integer>> res = new ArrayList<>();
|
||||
if (root == null) return res; // 非空判断
|
||||
|
||||
list<integer> path = new linkedlist<>();
|
||||
|
||||
List<Integer> path = new LinkedList<>();
|
||||
preorderdfs(root, targetsum, res, path);
|
||||
return res;
|
||||
}
|
||||
|
||||
public void preorderdfs(treenode root, int targetsum, list<list<integer>> res, list<integer> path) {
|
||||
public void preorderdfs(TreeNode root, int targetsum, List<List<Integer>> res, List<Integer> path) {
|
||||
path.add(root.val);
|
||||
// 遇到了叶子节点
|
||||
if (root.left == null && root.right == null) {
|
||||
// 找到了和为 targetsum 的路径
|
||||
if (targetsum - root.val == 0) {
|
||||
res.add(new arraylist<>(path));
|
||||
res.add(new ArrayList<>(path));
|
||||
}
|
||||
return; // 如果和不为 targetsum,返回
|
||||
}
|
||||
|
Reference in New Issue
Block a user