forked from illuz/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAC_dfs_n.java
49 lines (43 loc) · 1.3 KB
/
AC_dfs_n.java
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
40
41
42
43
44
45
46
47
48
49
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_dfs_n.java
* Create Date: 2015-02-21 09:23:19
* Descripton:
*/
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Solution {
// Definition for binary tree
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
private void dfs(TreeNode root, int sum, List<List<Integer>> res, List<Integer> path) {
if (root == null)
return;
path.add(root.val);
sum -= root.val;
if (root.left == null && root.right == null && sum == 0) {
res.add(new ArrayList<Integer>(path));
}
dfs(root.left, sum, res, path);
dfs(root.right, sum, res, path);
path.remove(path.size() - 1);
}
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
List<Integer> path = new ArrayList<Integer>();
dfs(root, sum, res, path);
return res;
}
// debug
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
Solution s = new Solution();
int[] input = {1, 2, 3, 1};
System.out.println("no case");
}
}