Find Kth largest number in a given Binary Tree
Given a Binary Tree consisting of N nodes and a positive integer K, the task is to find the Kth largest number in the given tree.
Examples:
Input: K = 3
1
/ \
2 3
/ \ / \
4 5 6 7
Output: 5
Explanation: The third largest element in the given binary tree is 5.
Input: K = 1
1
/ \
2 3
Output: 1
Explanation: The first largest element in the given binary tree is 1.
Naive Approach: Flatten the given binary tree and then sort the array. Print the Kth largest number from this sorted array now.
Efficient Approach: The above approach can be made efficient by storing all the elements of the Tree in a priority queue, as the elements are stored based on the priority order, which is ascending by default. Though the complexity will remain the same as the above approach, here we can avoid the additional sorting step.
Just print the Kth largest element in the priority queue.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node *left, *right;
};
Node* newNode( int data)
{
Node* temp = new Node();
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
int findKthLargest(priority_queue< int ,
vector< int > >& pq,
int k)
{
while (--k && !pq.empty()) {
pq.pop();
}
if (!pq.empty()) {
return pq.top();
}
return -1;
}
void traverse(
Node* root, priority_queue< int ,
vector< int > >& pq)
{
if (!root) {
return ;
}
pq.push(root->data);
traverse(root->left, pq);
traverse(root->right, pq);
}
void findKthLargestTree(Node* root, int K)
{
priority_queue< int , vector< int > > pq;
traverse(root, pq);
cout << findKthLargest(pq, K);
}
int main()
{
Node* root = newNode(1);
root->left = newNode(2);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->right = newNode(3);
root->right->right = newNode(7);
root->right->left = newNode(6);
int K = 3;
findKthLargestTree(root, K);
return 0;
}
|
Java
import java.util.*;
public class Main
{
static class Node
{
int data;
Node left;
Node right;
}
static Node newNode( int data)
{
Node temp = new Node();
temp.data = data;
temp.left = null ;
temp.right = null ;
return temp;
}
static Vector<Integer> pq = new Vector<Integer>();
static int findKthLargest( int k)
{
--k;
while (k > 0 && pq.size() > 0 ) {
pq.remove( 0 );
--k;
}
if (pq.size() > 0 ) {
return pq.get( 0 );
}
return - 1 ;
}
static void traverse(Node root)
{
if (root == null ) {
return ;
}
pq.add(root.data);
Collections.sort(pq);
Collections.reverse(pq);
traverse(root.left);
traverse(root.right);
}
static void findKthLargestTree(Node root, int K)
{
traverse(root);
System.out.print(findKthLargest(K));
}
public static void main(String[] args)
{
Node root = newNode( 1 );
root.left = newNode( 2 );
root.left.left = newNode( 4 );
root.left.right = newNode( 5 );
root.right = newNode( 3 );
root.right.right = newNode( 7 );
root.right.left = newNode( 6 );
int K = 3 ;
findKthLargestTree(root, K);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
pq = []
def findKthLargest(k):
k - = 1
while (k > 0 and len (pq) > 0 ):
pq.pop( 0 )
k - = 1
if len (pq) > 0 :
return pq[ 0 ]
return - 1
def traverse(root):
if (root = = None ):
return
pq.append(root.data)
pq.sort()
pq.reverse()
traverse(root.left)
traverse(root.right)
def findKthLargestTree(root, K):
traverse(root);
print (findKthLargest(K))
root = Node( 1 )
root.left = Node( 2 )
root.left.left = Node( 4 )
root.left.right = Node( 5 )
root.right = Node( 3 )
root.right.right = Node( 7 )
root.right.left = Node( 6 )
K = 3
findKthLargestTree(root, K)
|
C#
using System;
using System.Collections.Generic;
class GFG {
class Node
{
public int data;
public Node left, right;
};
static Node newNode( int data)
{
Node temp = new Node();
temp.data = data;
temp.left = null ;
temp.right = null ;
return temp;
}
static List< int > pq = new List< int >();
static int findKthLargest( int k)
{
--k;
while (k > 0 && pq.Count > 0) {
pq.RemoveAt(0);
--k;
}
if (pq.Count > 0) {
return pq[0];
}
return -1;
}
static void traverse(Node root)
{
if (root == null ) {
return ;
}
pq.Add(root.data);
pq.Sort();
pq.Reverse();
traverse(root.left);
traverse(root.right);
}
static void findKthLargestTree(Node root, int K)
{
traverse(root);
Console.Write(findKthLargest(K));
}
static void Main() {
Node root = newNode(1);
root.left = newNode(2);
root.left.left = newNode(4);
root.left.right = newNode(5);
root.right = newNode(3);
root.right.right = newNode(7);
root.right.left = newNode(6);
int K = 3;
findKthLargestTree(root, K);
}
}
|
Javascript
<script>
class Node
{
constructor(data) {
this .left = null ;
this .right = null ;
this .data = data;
}
}
function newNode(data)
{
let temp = new Node(data);
return temp;
}
let pq = [];
function findKthLargest(k)
{
--k;
while (k > 0 && pq.length > 0) {
pq.shift();
--k;
}
if (pq.length > 0) {
return pq[0];
}
return -1;
}
function traverse(root)
{
if (root == null ) {
return ;
}
pq.push(root.data);
pq.sort( function (a, b){ return a - b});
pq.reverse();
traverse(root.left);
traverse(root.right);
}
function findKthLargestTree(root, K)
{
traverse(root);
document.write(findKthLargest(K));
}
let root = newNode(1);
root.left = newNode(2);
root.left.left = newNode(4);
root.left.right = newNode(5);
root.right = newNode(3);
root.right.right = newNode(7);
root.right.left = newNode(6);
let K = 3;
findKthLargestTree(root, K);
</script>
|
Time Complexity: O((N + K)log N)
Auxiliary Space: O(N)
Last Updated :
24 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...