-
Notifications
You must be signed in to change notification settings - Fork 5
/
r3.cpp
150 lines (123 loc) · 2.86 KB
/
r3.cpp
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
143
144
145
146
147
148
149
150
// Java program for the above approch
import java.util.*;
class GFG{
// Node structure
static class node
{
int data;
node left = null;
node right = null;
}
// Creates and initilize a new node
static node newNode(int ch)
{
// Allocating memory to a new node
node n = new node();
n.data = ch;
n.left = null;
n.right = null;
return n;
}
// Function to find the height of tree
static int Height(node root)
{
if (root == null)
return 0;
return 1 + Math.max(Height(root.left),
Height(root.right));
}
// Given a perfect binary tree
// print its node in Specific order
static void printSpecificLevelOrder(Queue<node> A,
Queue<node> B,
int height)
{
while (height != 0)
{
// Get each one front
// element of both queue
node X = A.poll();
node Y = B.poll();
// Check if X exist or not
if (X == null)
{
// Assume is has and put
// their both child as none
A.add(null);
A.add(null);
}
else
{
// print the data and store
// their child first left
// then right
System.out.print(X.data + " ");
A.add(X.left);
A.add(X.right);
}
// Check Y exist or not
if (Y == null)
{
// Assume is has and put
// their both child as none
B.add(null);
B.add(null);
}
else
{
// Print the data and store their
// child first left then right
System.out.print(Y.data + " ");
B.add(Y.right);
B.add(Y.left);
}
// Decrease by 1 unit
height -= 1;
}
}
// Driver Code
public static void main (String[] args)
{
// Given tree
node root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.left = newNode(4);
root.left.right = newNode(5);
root.right.right = newNode(7);
root.left.left.left = newNode(8);
root.left.left.right = newNode(9);
root.left.right.left = newNode(10);
root.left.right.right = newNode(11);
root.right.right.left = newNode(14);
root.right.right.right = newNode(15);
root.left.left.left.left = newNode(16);
root.left.left.left.right = newNode(17);
root.left.left.right.left = newNode(18);
root.left.left.right.right = newNode(19);
root.left.right.left.left = newNode(20);
root.left.right.left.right = newNode(21);
root.left.right.right.left = newNode(22);
root.left.right.right.right = newNode(23);
root.right.right.left.left = newNode(28);
root.right.right.left.right = newNode(29);
root.right.right.right.left = newNode(30);
root.right.right.right.right = newNode(31);
// Initialise Queue
Queue<node> A = new LinkedList<>();
Queue<node> B = new LinkedList<>();
int height = 0;
// Check top root manually
if (root != null)
{
System.out.print(root.data + " ");
A.add(root.left);
B.add(root.right);
height = Height(root);
height = (int)Math.pow(2, (height - 1)) - 1;
}
// Function Call
printSpecificLevelOrder(A, B, height);
}
}
// This code is contributed by offbeat