-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathBT_Problem_16.java
More file actions
79 lines (63 loc) · 1.63 KB
/
Copy pathBT_Problem_16.java
File metadata and controls
79 lines (63 loc) · 1.63 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
package trees.binaryTree;
/*
* Problem Title :- Boundary traversal of a Binary tree
*/
public class BT_Problem_16 {
Node root;
// printing leaf nodes
void printLeaves(Node node) {
if(node == null)
return;
printLeaves(node.left);
// print if it is a leaf node
if(node.left == null && node.right == null)
System.out.print(node.data + " ");
printLeaves(node.right);
}
void printBoundaryLeft(Node node) {
if(node == null)
return;
if(node.left != null) {
System.out.print(node.data + " ");
printBoundaryLeft(node.left);
}
else if(node.right != null) {
System.out.print(node.data + " ");
printBoundaryLeft(node.right);
}
}
void printBoundaryRight(Node node) {
if(node == null)
return;
if(node.left != null) {
System.out.print(node.data + " ");
printBoundaryRight(node.left);
}
else if(node.right != null) {
System.out.print(node.data + " ");
printBoundaryRight(node.right);
}
}
void printBoundary(Node node) {
if(node == null)
return;
System.out.print(node.data + " ");
printBoundaryLeft(node.left);
printLeaves(node.left);
printLeaves(node.right);
printBoundaryRight(node.right);
}
public static void main(String[] args) {
BT_Problem_16 tree = new BT_Problem_16();
tree.root = new Node(1);
tree.root.left = new Node(2);
tree.root.left.left = new Node(4);
tree.root.left.right = new Node(5);
tree.root.left.left.left = new Node(8);
tree.root.left.right.left = new Node(10);
tree.root.left.right.right = new Node(14);
tree.root.right = new Node(3);
tree.root.right.right = new Node(22);
tree.printBoundary(tree.root);
}
}