-
Notifications
You must be signed in to change notification settings - Fork 148
/
Second largest element in tree.cpp
71 lines (56 loc) · 1.89 KB
/
Second largest element in tree.cpp
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
/*
Second Largest Element In Tree
Given a generic tree, find and return the node with second largest value in given tree.
Note: Return NULL if no node with required value is present.
Input format :
The first line of input contains data of the nodes of the tree in level order form. The order is: data for root node, number of children to root node,
data of each of child nodes and so on and so forth for each node. The data of the nodes of the tree is separated by space.
Output Format :
The first and only line of output contains data of the node with second largest data.
Constraints:
Time Limit: 1 sec
Sample Input 1 :
10 3 20 30 40 2 40 50 0 0 0 0
Sample Output 1 :
40
*/
#include<vector>
#include<utility>
#include<climits>
TreeNode<int> * findMax(TreeNode<int> *a, TreeNode<int> *b) {
if(a -> data > b -> data) {
return a;
} else {
return b;
}
}
pair<TreeNode<int>*,TreeNode<int>*> helper(TreeNode<int> *root) {
TreeNode<int> *max_1 = root;
TreeNode<int> *max_2 = new TreeNode<int>(INT_MIN);
for(int i = 0; i < root -> children.size(); i++) {
pair<TreeNode<int>*,TreeNode<int>*> temp = helper(root -> children[i]);
TreeNode<int>* discard;
if(max_1 -> data < temp.first -> data) {
discard = max_1;
max_1 = temp.first;
if(max_2) {
max_2 = findMax(findMax(discard,temp.second), max_2);
} else {
max_2 = findMax(discard,temp.second);
}
} else if(max_2 -> data < temp.first -> data ) {
max_2 = temp.first;
}
}
if(max_1 -> data != max_2 -> data) {
return {max_1,max_2};
} else {
return {max_1,NULL};
}
}
TreeNode<int> * getSecondLargestNode(TreeNode<int> *root) {
if(root == NULL or root -> children.size() == 0) {
return NULL;
}
return helper(root).second;
}