MCPcopy Index your code
hub / github.com/careercup/ctci / findSum

Method findSum

java/Chapter 4/Question4_9/Question.java:6–29  ·  view source on GitHub ↗
(TreeNode node, int sum, int[] path, int level)

Source from the content-addressed store, hash-verified

4public class Question {
5
6 public static void findSum(TreeNode node, int sum, int[] path, int level) {
7 if (node == null) {
8 return;
9 }
10
11 /* Insert current node into path */
12 path[level] = node.data;
13
14 int t = 0;
15 for (int i = level; i >= 0; i--){
16 t += path[i];
17 if (t == sum) {
18 print(path, i, level);
19 }
20 }
21
22 findSum(node.left, sum, path, level + 1);
23 findSum(node.right, sum, path, level + 1);
24
25 /* Remove current node from path. Not strictly necessary, since we would
26 * ignore this value, but it's good practice.
27 */
28 path[level] = Integer.MIN_VALUE;
29 }
30
31 public static int depth(TreeNode node) {
32 if (node == null) {

Callers 1

mainMethod · 0.95

Calls 2

printMethod · 0.95
depthMethod · 0.95

Tested by

no test coverage detected