-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinSecondRank_binarytree.java
124 lines (103 loc) · 2.83 KB
/
MinSecondRank_binarytree.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
// In a Marketing Agency, each agent will mentor either two sub-agents,
// or zero agents. Now, based on ranks given to sub-agents, the mentor agent
// will be ranked with the top rank among two sub-agents.
// The ranks are in the range [1,20], More than one agent can have same rank.
// At the end, all mentor agents and sub agents, will be treated as agents only.
// You are given the entire ranking picture as a tree.
// Your task is to find out second top agent in the Marketing agency.
// If no such agent exist, return -2.
// Implement the class Solution:
// 1. public int findSecondTopAgent(BinaryTreeNode root): returns an integer.
// NOTE:
// - In the tree '-1', indicates empty(null).
// Input Format:
// -------------
// A single line of space separated integers, ranks of each individual.
// Output Format:
// --------------
// Print an integer, second top agent based on rank.
// Sample Input-1:
// ---------------
// 2 5 2 -1 -1 2 4
// Sample Output-1:
// ----------------
// 4
// Sample Input-2:
// ---------------
// 3 3 3 3 3
// Sample Output-2:
// ----------------
// -2
// ::For Tree structure refer to Hint::
solution1:
import java.util.*;
/*
class BinaryTreeNode{
public int data;
public BinaryTreeNode left, right;
public BinaryTreeNode(int data){
this.data = data;
left = null;
right = null;
}
}
*/
class Solution {
static List<Integer> l=new ArrayList<>();
static void inorder(BinaryTreeNode root){
if(root!=null && root.data!=-1){
inorder(root.left);
if(!l.contains(root.data))
l.add(root.data);
inorder(root.right);
}
}
public int findSecondTopAgent(BinaryTreeNode root) {
//implement your code here.
inorder(root);
Collections.sort(l);
if(l.size()>2){
return l.get(1);
}
return -2;
}
}
solution2:
import java.util.*;
/*
class BinaryTreeNode{
public int data;
public BinaryTreeNode left, right;
public BinaryTreeNode(int data){
this.data = data;
left = null;
right = null;
}
}
*/
class Solution {
public int findSecondTopAgent(BinaryTreeNode root) {
Queue<BinaryTreeNode> q=new LinkedList<>();
q.add(root);
ArrayList<Integer> l=new ArrayList<>();
while(!q.isEmpty()){
BinaryTreeNode front=q.peek();
q.poll();
if(!l.contains(front.data)){
l.add(front.data);
}
if(front.left!=null && front.left.data!=-1){
q.add(front.left);
}
if(front.right!=null && front.right.data!=-1){
q.add(front.right);
}
}
int mini1=212345;
for(int i=0;i<l.size();i++){
if(l.get(i)<mini1){
mini1=l.get(i);
}
}
int mini2=1234567;
for(int i=0;i<l.size();i++){