-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChristmas_levelOrder.java
145 lines (126 loc) · 3.99 KB
/
Christmas_levelOrder.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
143
144
145
// For X-Mas, santa claus is preparing a X-Mas Tree with set of Bulbs.
// The bulbs are of different voltages, and preparation of tree as follows:
// - The bulbs are arranged in level-wise, levels are numbered from 0,1,2,3..
// so on.
// - At level-0: There will be only one bulb as root bulb.,
// - From next level onwards, we can attach atmost two bulbs to left side,
// and right side of every bulb in previous level.
// - The empty attachements in a level are indicated with -1.
// (for example: look in hint)
// Entire X-Mas tree has to be prepared with certian rules as follows:
// - For every even level in the X-Mas Tree, all the bulbs should have
// odd voltages in strictly ascending order.
// - For every odd level in the X-Mas Tree, all the bulbs should have
// even voltages in strictly descending order.
// You will be given the X-Mas Tree root,
// Your task is to findout whether the X-Mas tree is prepared as per the rules
// or not.
// Implement the class Solution.
// 1.public boolean isXmasTree(BinaryTreeNode root): returns a boolean value.
// Input Format:
// -------------
// A single line of space separated integers, voltages of the set of bulbs.
// Output Format:
// --------------
// Print a boolean value.
// Sample Input-1:
// ---------------
// 3 8 4 3 5 -1 7
// Sample Output-1:
// ----------------
// true
// Sample Input-2:
// ---------------
// 3 8 4 3 5 7 7
// Sample Output-2:
// ----------------
// false
// Sample Input-3:
// ---------------
// 1
// Sample Output-3:
// ----------------
// true
/*
/*
//TreeNode Structure for Your Reference..
class BinaryTreeNode{
public int data;
public BinaryTreeNode left, right;
public BinaryTreeNode(int data){
this.data = data;
left = null;
right = null;
}
}
*/
import java.util.*;
class Solution {
public boolean isXmasTree(BinaryTreeNode root) {
// Implement Your Code here..
Queue<BinaryTreeNode> q=new LinkedList<>();
q.add(root);
int level=0;
while(!q.isEmpty()){
List<Integer> l=new ArrayList<>();
int c=q.size();
while(c>0){
BinaryTreeNode front=q.peek();
l.add(front.data);
q.poll();
if(front.left!=null && front.left.data!=-1){
q.add(front.left);
}
if(front.right!=null && front.right.data!=-1){
q.add(front.right);
}
c--;
}
System.out.println(l);
if(level%2!=0){
// System.out.println(l);
for(int i=0;i<l.size()-1;i++){
if(l.get(i)%2==0){
if(l.get(i)>l.get(i+1)){
continue;
}
else{
// System.out.println(false);
// System.exit(0);
return false;
}
}
else{
// System.out.println(false);
// System.exit(0);
return false;
}
}
}
else if(level%2==0){
// System.out.prin
// tln(l);
for(int i=0;i<l.size()-1;i++){
if(l.get(i)%2!=0){
if(l.get(i)<l.get(i+1)){
continue;
}
else{
// System.out.println(false);
// System.exit(0);
return false;
}
}
else{
// System.out.println(false);
// System.exit(0);
return false;
}
}
}
l.clear();
level++;
}
return true;
}
}