diff --git a/src/Traverse.java b/src/Traverse.java index 7945513..f26586b 100644 --- a/src/Traverse.java +++ b/src/Traverse.java @@ -28,6 +28,49 @@ public static void main(String[] args) { v45.neighbors = new ArrayList<>(List.of(v23)); v23.neighbors = new ArrayList<>(List.of()); v67.neighbors = new ArrayList<>(List.of(v91)); + + //traverse(v3); + int result = sum(v45); + System.out.println(result); + } + + public static int sum(Vertex current) { + Set> myVisited = new HashSet<>(); + + return sum(current, myVisited); + } + + public static int sum(Vertex current, Set> visited) { + if (current == null || visited.contains(current)) return 0; + + visited.add(current); + + int total = 0; + total += current.data; + + for (Vertex neighbor : current.neighbors) { + int neighborSum = sum(neighbor, visited); // dont forget to do something with the returned result of our recursive call + total += neighborSum; + } + + return total; } + public static void traverse(Vertex current) { + Set> myVisited = new HashSet<>(); + traverseHelper(current, myVisited); + } + + // Core of our algorithm: THE most important to know for technical interviews + // - expects us to understand very thoroughly; as core as using for loop to go thru array + public static void traverseHelper(Vertex current, Set> visited) { + if (current == null || visited.contains(current)) return; + + System.out.println(current.data); + visited.add(current); + + for(Vertex neighbor : current.neighbors) { + traverseHelper(neighbor, visited); + } + } }