-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCountAllPathSum.java
More file actions
48 lines (48 loc) · 1.84 KB
/
CountAllPathSum.java
File metadata and controls
48 lines (48 loc) · 1.84 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
//import java.util.*;
//
//
//class CountAllPathSum {
// public static int countPaths(TreeNode root, int S) {
// List<Integer> currentPath = new LinkedList<>();
// return countPathsRecursive(root, S, currentPath);
// }
//
// private static int countPathsRecursive(TreeNode currentNode, int S, List<Integer> currentPath) {
// if (currentNode == null)
// return 0;
//
// // add the current node to the path
// currentPath.add(currentNode.val);
// int pathCount = 0, pathSum = 0;
// // find the sums of all sub-paths in the current path list
// ListIterator<Integer> pathIterator = currentPath.listIterator(currentPath.size());
// while (pathIterator.hasPrevious()) {
// pathSum += pathIterator.previous();
// // if the sum of any sub-path is equal to 'S' we increment our path count.
// if (pathSum == S) {
// pathCount++;
// }
// }
//
// // traverse the left sub-tree
// pathCount += countPathsRecursive(currentNode.left, S, currentPath);
// // traverse the right sub-tree
// pathCount += countPathsRecursive(currentNode.right, S, currentPath);
//
// // remove the current node from the path to backtrack,
// // we need to remove the current node while we are going up the recursive call stack.
// currentPath.remove(currentPath.size() - 1);
//
// return pathCount;
// }
//
// public static void main(String[] args) {
// TreeNode root = new TreeNode(12);
// root.left = new TreeNode(7);
// root.right = new TreeNode(1);
// root.left.left = new TreeNode(4);
// root.right.left = new TreeNode(10);
// root.right.right = new TreeNode(5);
// System.out.println("Tree has path: " + CountAllPathSum.countPaths(root, 11));
// }
//}