forked from anitaa1990/Android-Cheat-sheet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrintTreePerimeter.java
142 lines (102 loc) · 3.34 KB
/
PrintTreePerimeter.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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package trees;
import java.util.Stack;
public class PrintTreePerimeter {
/*
* Given the root node of a binary tree, print nodes forming the boundary (perimeter).
*
*
* Runtime Complexity:
* Linear, O(n)
*
* Memory Complexity:
* O(h)
*
* Print the root node.
* Print the left boundary in top-down order.
* Print the leaf nodes in left-right order.
* Print the right boundary in bottom-up order.
* Push node values in a stack here. Once the leaf node is hit, pop all elements of the stack
* while printing them.
*
* */
private static class Node {
private int data;
private Node left, right;
Node(int item) {
data = item;
left = right = null;
}
public int getData() {
return data;
}
public Node getLeft() {
return left;
}
public Node getRight() {
return right;
}
}
private Node root1;
protected static void printLeftPerimeter(Node root) {
while (root != null) {
int curr_val = root.data;
if (root.left != null) {
root = root.left;
} else if (root.right != null) {
root = root.right;
} else {
break;
}
System.out.print(curr_val + " ");
}
}
public static void printRightPerimeter(Node root) {
Stack<Integer> rightValues = new Stack<>();
while (root != null) {
int curr_val = root.data;
if (root.right != null) {
root = root.right;
} else if (root.left != null) {
root = root.left;
} else {
break; // leaf node.
}
rightValues.push(curr_val);
}
while (!rightValues.isEmpty()) {
System.out.print(rightValues.pop() + " ");
}
}
protected static void printLeafNodes(Node root) {
if (root != null) {
printLeafNodes(root.left);
printLeafNodes(root.right);
if (root.left == null &&
root.right == null) {
System.out.print(root.data + " ");
}
}
}
protected static void displayTreePerimeter(Node root) {
if (root != null) {
System.out.print(root.data + " ");
printLeftPerimeter(root.left);
if (root.left != null || root.right != null) {
printLeafNodes(root);
}
printRightPerimeter(root.right);
}
}
public static void main(String[] argv) {
PrintTreePerimeter printTreePerimeter = new PrintTreePerimeter();
printTreePerimeter.root1 = new Node(1);
printTreePerimeter.root1.left = new Node(2);
printTreePerimeter.root1.right = new Node(3);
printTreePerimeter.root1.left.left = new Node(4);
printTreePerimeter.root1.left.right = new Node(5);
printTreePerimeter.root1.right.left = new Node(6);
printTreePerimeter.root1.right.right = new Node(7);
System.out.print("Perimeter:\n");
displayTreePerimeter(printTreePerimeter.root1);
}
}