-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoundaryTraversalOfTree.java
49 lines (42 loc) · 1.55 KB
/
BoundaryTraversalOfTree.java
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
package com.anudev.ds.trees;
public class BoundaryTraversalOfTree {
public static void printBoundaryTraversal(Node node) {
System.out.print("Boundary traversal: " + node.getValue() + ",");
printLeftBoundary(node.getLeftNode());
printRightBoundary(node.getRightNode());
printLeafNodes(node);
}
private static void printLeftBoundary(Node node) {
if (node != null) {
if (node.getLeftNode() != null) {
System.out.print(node.getValue() + ",");
printLeftBoundary(node.getLeftNode());
} else if (node.getRightNode() != null) {
System.out.print(node.getValue() + ",");
printLeftBoundary(node.getRightNode());
}
}
}
private static void printRightBoundary(Node node) {
if (node != null) {
if (node.getRightNode() != null) {
System.out.print(node.getValue() + ",");
printRightBoundary(node.getRightNode());
} else if (node.getLeftNode() != null) {
System.out.print(node.getValue() + ",");
printRightBoundary(node.getLeftNode());
}
}
}
private static void printLeafNodes(Node node) {
if (node == null) {
return;
}
if (node.getLeftNode() == null && node.getRightNode() == null) {
System.out.print(node.getValue() + ",");
} else {
printLeafNodes(node.getLeftNode());
printLeafNodes(node.getRightNode());
}
}
}