forked from prashantkalokhe/Hactoberfest-2022-New
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPerfectBinery.java
58 lines (47 loc) · 1.17 KB
/
PerfectBinery.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
class Node{
int value;
Node left;
Node right;
public Node(int value){
this.value = value;
left = right = null;
}
}
public class PerfectBinary {
Node node;
PerfectBinary(int value){
node = new Node(value);
}
PerfectBinary(){
node = null;
}
public static int depth(Node node){
int d = 0;
while (node != null){
d += 1;
node = node.left;
}
return d;
}
public static boolean perfect(Node node , int d , int l ){
if(node == null){
return true;
}
if(node.left == null && node.right == null){
return d == l + 1;
}
if(node.left == null || node.right == null){
return false;
}
return (perfect(node.left,d,l + 1) && perfect(node.right , d , l + 1));
}
public static void main(String[] args){
Node root = null;
root = new Node(2);
root.left = new Node(3);
root.right = new Node(4);
root.right.right = new Node(7);
System.out.println(depth(root));
System.out.println(perfect(root, depth(root),0));
}
}