Repository navigation
Expand file tree
/
Copy pathPathSumII.java
More file actions
71 lines (63 loc) · 1.67 KB
/
Copy pathPathSumII.java
File metadata and controls
71 lines (63 loc) · 1.67 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.
For example:
Given the below binary tree and sum = 22,
5
/ \
4 8
/ / \
11 13 4
/ \ / \
7 2 5 1
return
[
[5,4,11,2],
[5,8,4,5]
]
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> list=new ArrayList<List<Integer>>();
LinkedList<TreeNode> stack=new LinkedList<TreeNode>();
int total=0;
TreeNode node=root;
TreeNode visited=null;
while(!stack.isEmpty()|| node!=null)
{
while(node!=null)
{
total+=node.val;
stack.push(node);
node=node.left;
}
node=stack.peek();
if(total==sum&&node.right==null&&node.left==null)
{
ArrayList<Integer> alist=new ArrayList<Integer>();
for(ListIterator<TreeNode> it=stack.listIterator(0);it.hasNext();)
alist.add(it.next().val);
Collections.reverse(alist);
list.add(alist) ;
}
if(node.right==null || visited==node.right)
{
total-=node.val;
visited=node;
node=null;
stack.pop();
}
else
node=node.right;
}
return list;
}
}