-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
122 lines (113 loc) · 3.61 KB
/
Copy pathMain.java
File metadata and controls
122 lines (113 loc) · 3.61 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
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
import com.sun.source.tree.Tree;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
static int n;
static int level=1;
static int width=0;
static int count=1;
static StringBuffer sb;
static int[][] insert;
public static void main(String[] args)throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(br.readLine());
sb = new StringBuffer();
insert = new int[n+1][2];
StringTokenizer st;
int value;
int left;
int right;
List<Integer> parent = new ArrayList<>();
HashSet child = new HashSet();
for(int i=1;i<=n;i++){
st = new StringTokenizer(br.readLine());
value = Integer.parseInt(st.nextToken());
left = Integer.parseInt(st.nextToken());
right = Integer.parseInt(st.nextToken());
insert[value][0]=left;
insert[value][1]=right;
parent.add(value);
child.add(left);
child.add(right);
}
int rootnum=0;
for(int i: parent){
if(!child.contains(i)){
rootnum=i;
break;
}
}
TreeNode root = new TreeNode(rootnum,null,null);
makeTreeNode(root,rootnum,insert[rootnum][0],insert[rootnum][1]);
positionCount(root);
bfs(root);
System.out.println(level+" "+(width+1));
}
static void makeTreeNode(TreeNode Node,int value,int left,int right){
if(Node.value==value){
if(left!=-1){
Node.leftChild = left ==-1?null:new TreeNode(left,null,null);
}
if(right!=-1){
Node.rightChild = right ==-1?null:new TreeNode(right,null,null);
}
}
if(Node.leftChild!=null)makeTreeNode(Node.leftChild,Node.leftChild.value,insert[Node.leftChild.value][0],insert[Node.leftChild.value][1]);
if(Node.rightChild!=null)makeTreeNode(Node.rightChild,Node.rightChild.value,insert[Node.rightChild.value][0],insert[Node.rightChild.value][1]);
}
static void bfs(TreeNode root){
Queue<TreeNode> q = new LinkedList<>();
q.offer(root);
int levelcount=1;
while(!q.isEmpty()){
int size = q.size();
int x=0;
for(int i=0;i<size;i++){
TreeNode node = q.poll();
if(node.leftChild!=null) {
q.offer(node.leftChild);
}
if(node.rightChild!=null) {
q.offer(node.rightChild);
}
if(i==0){
x=node.x;
}
if(i==size-1){
x = node.x-x;
if(width<x){
width=x;
level=levelcount;
}
}
}
levelcount++;
}
}
static void positionCount(TreeNode Node){
if(Node.leftChild!=null){
positionCount(Node.leftChild);
}
Node.position(count);
count++;
if(Node.rightChild!=null){
positionCount(Node.rightChild);
}
}
static class TreeNode{
int value;
TreeNode leftChild;
TreeNode rightChild;
int x;
TreeNode(int value,TreeNode left,TreeNode right){
this.value=value;
this.leftChild=left;
this.rightChild=right;
}
void position(int x){
this.x=x;
}
}
}