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