-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrintEachRootToLeafPath.java
More file actions
38 lines (29 loc) · 963 Bytes
/
PrintEachRootToLeafPath.java
File metadata and controls
38 lines (29 loc) · 963 Bytes
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
package com.anudev.ds.trees;
import java.util.LinkedList;
public class PrintEachRootToLeafPath {
public static LinkedList<Node> linkedList = new LinkedList();
public static void printAllPaths(Node node) {
System.out.println("All root to leaf paths: ");
findAllRootToLeafPath(node);
}
private static void findAllRootToLeafPath(Node node) {
if (node == null) {
return;
}
// add to linked list
linkedList.add(node);
if (node.getLeftNode() == null && node.getRightNode() == null) {
printLinkedList();
} else {
findAllRootToLeafPath(node.getLeftNode());
findAllRootToLeafPath(node.getRightNode());
}
linkedList.removeLast();
}
private static void printLinkedList() {
for (Node node : linkedList) {
System.out.print(node.getValue() + ",");
}
System.out.println();
}
}