forked from xiaoningning/java-algorithm-2010
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeLeafPath.java
More file actions
98 lines (81 loc) · 2.48 KB
/
TreeLeafPath.java
File metadata and controls
98 lines (81 loc) · 2.48 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import java.util.ArrayList;
import java.util.Stack;
import java.util.List;
/**
* print out all leaf node path with non recursive method
* The structure of node is *NOT* binary tree, is just a normal tree.
* <p/>
* print out all leaf node path
* // 12
* // 4 8 22
* //1 2 3 9 18 24
* <p/>
* the output is like:
* <p/>
* 12, 4, 1,
* 12, 4, 2,
* 12, 4, 3,
* 12, 4, 8, 9,
* 12, 4, 8, 22, 18,
* 12, 4, 8, 22, 24,
*/
public class TreeLeafPath {
public Integer value;
public ArrayList<TreeLeafPath> children;
public TreeLeafPath(Integer v) {
value = v;
children = new ArrayList<TreeLeafPath>();
}
public void addTree(TreeLeafPath node) {
children.add(node);
}
public static void printPath(Stack<TreeLeafPath> s) {
for (int i = 0; i < s.size(); i++) {
if (i + 1 == s.size())
System.out.print(s.get(i).value);
else
System.out.print(s.get(i).value + "->");
}
System.out.println();
}
public static void leafPath(TreeLeafPath node, Stack<TreeLeafPath> stack) {
stack.push(node);
TreeLeafPath current = stack.peek();
ArrayList<TreeLeafPath> children = current.children;
if (children.size() == 0) {
printPath(stack);
stack.pop();
return;
} else {
for (TreeLeafPath c : children) {
leafPath(c, stack);
}
stack.pop();
return;
}
}
public static void main(String[] args) {
TreeLeafPath root = new TreeLeafPath(12);
TreeLeafPath node4 = new TreeLeafPath(4);
TreeLeafPath node8 = new TreeLeafPath(8);
TreeLeafPath node1 = new TreeLeafPath(1);
TreeLeafPath node2 = new TreeLeafPath(2);
TreeLeafPath node3 = new TreeLeafPath(3);
TreeLeafPath node9 = new TreeLeafPath(9);
TreeLeafPath node18 = new TreeLeafPath(18);
TreeLeafPath node22 = new TreeLeafPath(22);
TreeLeafPath node16 = new TreeLeafPath(16);
node18.addTree(node16);
node18.addTree(node22);
node8.addTree(node9);
node8.addTree(node18);
node4.addTree(node1);
node4.addTree(node2);
node4.addTree(node3);
root.addTree(node4);
root.addTree(node8);
Stack<TreeLeafPath> stack = new Stack<TreeLeafPath>();
leafPath(root, stack);
System.out.println();
}
}