-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathceiling.java
More file actions
91 lines (89 loc) · 2.44 KB
/
ceiling.java
File metadata and controls
91 lines (89 loc) · 2.44 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
80
81
82
83
84
85
86
87
88
89
90
91
import java.util.*;
public class ceiling{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String testcase[] = scan.nextLine().split(" ");
int number = Integer.parseInt(testcase[0]);
Set<String> set = new HashSet<String>();
for (int i=0 ;i< number; i++){
BST tree = new BST();
String input[] = scan.nextLine().split(" ");
for (String line : input){
tree.insert(Integer.parseInt(line));
}
set.add(tree.traverse());
}
System.out.println(set.size());
scan.close();
}
public static boolean isSame(Node root1, Node root2){
if(root1 == null && root2 == null){
return true;
}
if((root1 == null && root2 !=null) || (root1 !=null && root2 == null)){
return false;
}
return isSame(root1.left, root2.left) && isSame(root1.right, root2.right);
}
}
class Node{
public Node left;
public Node right;
public int data;
public Node(int data){
this.left = null;
this.right = null;
this.data = data;
}
}
class BST{
public Node root;
public BST(){
this.root = null;
}
public void insert(int data){
if (this.root == null){
this.root = new Node(data);
}
else{
insertNode(data, root);
}
}
public void insertNode(int data, Node current){
if(current.data <= data){
if(current.right == null){
current.right = new Node(data);
}
else{
insertNode(data, current.right);
}
}
else {
if(current.left == null){
current.left = new Node(data);
}
else{
insertNode(data, current.left);
}
}
}
public String traverse(){
if (this.root == null){
return "";
}
StringBuilder str = new StringBuilder();
inorderTraverse(root, str);
return str.toString();
}
public void inorderTraverse(Node current,StringBuilder str){
if(current.left != null){
str.append('l');
inorderTraverse(current.left,str);
}
str.append('x');
if(current.right != null){
str.append('r');
inorderTraverse(current.right,str);
}
}
}