-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path437.path-sum-iii.java
56 lines (55 loc) · 1.63 KB
/
437.path-sum-iii.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
50
51
52
53
54
55
56
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
int res;
public int pathSum(TreeNode root, int sum) {
TreeNode head = root;
res = 0;
if(head == null)
return res;
Stack<TreeNode> stk = new Stack<TreeNode>();
stk.push(head);
while(!stk.isEmpty()){
TreeNode temp = stk.pop();
dfs(temp, 0, sum, new StringBuilder());
if(temp.left != null)
stk.push(temp.left);
if(temp.right != null)
stk.push(temp.right);
}
// dfs(root, 0, sum, new StringBuilder());
return res;
}
public void dfs(TreeNode root, int currsum, int sum, StringBuilder str){
if(root == null)
return;
currsum += root.val;
str.append(root.val+" ->");
//System.out.println(root.val+" path "+str.toString());
if(sum == currsum){
// System.out.println(root.val+" "+str.toString());
res++;
}
if(root.left != null){
// dfs(root.left, 0, sum, new StringBuilder());
dfs(root.left, currsum, sum, new StringBuilder(str));
}
if(root.right != null){
// dfs(root.right, 0, sum, new StringBuilder());
dfs(root.right, currsum, sum, new StringBuilder(str));
}
}
}