📌  相关文章
📜  在给定的二叉树中找到第 K 个最大的数

📅  最后修改于: 2021-10-28 02:08:02             🧑  作者: Mango

给定一个由N 个节点和一个正整数K组成的二叉树,任务是在给定的树中找到第 K最大的数。
例子:

朴素的方法:展平给定的二叉树,然后对数组进行排序。现在打印此排序数组中的第 K 个最大数。

高效方法:上述方法可以通过将树的所有元素存储在优先级队列中来提高效率,因为元素是根据优先级顺序存储的,默认情况下是升序。虽然复杂度将与上述方法保持一致,但在这里我们可以避免额外的排序步骤。
只需打印优先级队列中的K 个最大元素。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Struct binary tree node
struct Node {
    int data;
    Node *left, *right;
};
 
// Function to create a new node of
// the tree
Node* newNode(int data)
{
    Node* temp = new Node();
    temp->data = data;
    temp->left = temp->right = NULL;
    return temp;
}
 
// Utility function to find the Kth
// largest element in the given tree
int findKthLargest(priority_queue >& pq,
                   int k)
{
    // Loop until priority queue is not
    // empty and K greater than 0
    while (--k && !pq.empty()) {
        pq.pop();
    }
 
    // If PQ is not empty then return
    // the top element
    if (!pq.empty()) {
        return pq.top();
    }
 
    // Otherwise, return -1
    return -1;
}
 
// Function to traverse the given
// binary tree
void traverse(
    Node* root, priority_queue >& pq)
{
 
    if (!root) {
        return;
    }
 
    // Pushing values in binary tree
    pq.push(root->data);
 
    // Left and Right Recursive Call
    traverse(root->left, pq);
    traverse(root->right, pq);
}
 
// Function to find the Kth largest
// element in the given tree
void findKthLargestTree(Node* root, int K)
{
 
    // Stores all elements tree in PQ
    priority_queue > pq;
 
    // Function Call
    traverse(root, pq);
 
    // Function Call
    cout << findKthLargest(pq, K);
}
 
// Driver Code
int main()
{
    // Given Input
    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;
}


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG {
     
    // Struct binary tree node
    class Node
    {
        public int data;
        public Node left, right;
    };
     
    // Function to create a new node of the tree
    static Node newNode(int data)
    {
      Node temp = new Node();
      temp.data = data;
      temp.left = null;
      temp.right = null;
      return temp;
    }
     
    // Stores all elements tree in PQ
    static List pq = new List();
     
    // Utility function to find the Kth
    // largest element in the given tree
    static int findKthLargest(int k)
    {
        // Loop until priority queue is not
        // empty and K greater than 0
        --k;
        while (k > 0 && pq.Count > 0) {
            pq.RemoveAt(0);
            --k;
        }
       
        // If PQ is not empty then return
        // the top element
        if (pq.Count > 0) {
            return pq[0];
        }
       
        // Otherwise, return -1
        return -1;
    }
       
    // Function to traverse the given
    // binary tree
    static void traverse(Node root)
    {
       
        if (root == null) {
            return;
        }
       
        // Pushing values in binary tree
        pq.Add(root.data);
        pq.Sort();
        pq.Reverse();
       
        // Left and Right Recursive Call
        traverse(root.left);
        traverse(root.right);
    }
       
    // Function to find the Kth largest
    // element in the given tree
    static void findKthLargestTree(Node root, int K)
    {
        // Function Call
        traverse(root);
       
        // Function Call
        Console.Write(findKthLargest(K));
    }
 
  static void Main() {
    // Given Input
    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);
  }
}
 
// This code is contributed by divyeshrabadiya07.


Javascript


输出:
5

时间复杂度: O((N + K)log N)
辅助空间: O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程。