#include <bits/stdc++.h>
using
namespace
std;
class
Node {
public
:
int
data;
vector<Node*> childs;
};
int
largestELe = INT_MIN;
void
largestEleUnderRange(
Node* root,
int
data)
{
if
(root->data < data) {
largestELe = max(root->data,
largestELe);
}
for
(Node* child : root->childs) {
largestEleUnderRange(child, data);
}
}
void
KthLargestElement(Node* root,
int
K)
{
int
ans = INT_MAX;
for
(
int
i = 0; i < K; i++) {
largestEleUnderRange(root, ans);
ans = largestELe;
largestELe = INT_MIN;
}
cout << ans;
}
Node* newNode(
int
data)
{
Node* temp =
new
Node();
temp->data = data;
return
temp;
}
int
main()
{
Node* root = newNode(10);
(root->childs).push_back(newNode(2));
(root->childs).push_back(newNode(34));
(root->childs).push_back(newNode(56));
(root->childs).push_back(newNode(100));
(root->childs[0]->childs).push_back(newNode(77));
(root->childs[0]->childs).push_back(newNode(88));
(root->childs[2]->childs).push_back(newNode(1));
(root->childs[3]->childs).push_back(newNode(7));
(root->childs[3]->childs).push_back(newNode(8));
(root->childs[3]->childs).push_back(newNode(9));
int
K = 3;
KthLargestElement(root, K);
return
0;
}
Original page link
Best Cool Tech Gadgets
Top favorite technology gadgets
0 comments:
Post a Comment