N叉树中的第K个最小元素
给定一个 N 数组树(通用树)和一个整数K ,任务是找到第K个最小的 N 数组树中的元素。
例子:
Input: 10
/ / \ \
2 34 56 100
/ \ | / | \
77 88 1 7 8 9
K = 3
Output: 7
Explanation: 7 is the 3rd smallest element in the tree. The first two smallest elements are 1 and 2 respectively.
Input: 1
/ \ \
2 3 4
/ \
5 6
K = 4
Output: 4
方法:可以通过在给定范围内查找最小元素K次来解决该问题,并不断将范围的上端更新为迄今为止找到的最小元素。请按照以下步骤解决问题:
- 初始化一个全局变量,比如将 MinimumElement 设置为INT_MAX 。
- 声明一个函数minimumEleUnderRange(root, data)并执行以下操作:
- 如果root.data大于data ,则将MinimumElement更新为MinimumElement和root.data 的最小值。
- 遍历根的所有孩子。调用递归函数minimumEleUnderRange(child, data)。
- 声明一个函数KthSmallestElement(root, k)来执行以下操作:
- 初始化一个变量,比如ans为INT_MIN ,以存储第K个最小的元素。
- 使用变量i迭代范围[0, K – 1]并执行以下操作:
- 调用 minimumEleUnderRange (root, ans)函数,然后将ans更新为MinimumElement ,然后将MinimumElement更新为INT_MAX。
- 最后,打印ans作为所需答案。
下面是上述方法的实现。
C++
// C++ program for the above approach
#include
using namespace std;
// Structure of a node
class Node {
public:
int data;
vector childs;
};
// Global variable set to Maximum
int MinimumElement = INT_MAX;
// Function that gives the smallest
// element under the range of key
void smallestEleUnderRange(Node* root,
int data)
{
if (root->data > data) {
MinimumElement = min(
root->data, MinimumElement);
}
for (Node* child : root->childs) {
smallestEleUnderRange(child, data);
}
}
// Function to find the Kth smallest element
int kthSmallestElement(Node* root, int k)
{
int ans = INT_MIN;
for (int i = 0; i < k; i++) {
smallestEleUnderRange(root, ans);
ans = MinimumElement;
MinimumElement = INT_MAX;
}
return ans;
}
// Function to create a new node
Node* newNode(int data)
{
Node* temp = new Node();
temp->data = data;
return temp;
}
// Driver Code
int main()
{
/* Let us create below tree
* 10
* / / \ \
* 2 34 56 100
* / \ | / | \
* 77 88 1 7 8 9
*/
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));
cout << kthSmallestElement(root, 3);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
public class Main
{
// Class containing left and
// right child of current
// node and key value
static class Node {
public int data;
public Vector childs;
public Node(int data)
{
this.data = data;
childs = new Vector();
}
}
// Global variable set to Maximum
static int MinimumElement = Integer.MAX_VALUE;
// Function that gives the smallest
// element under the range of key
static void smallestEleUnderRange(Node root, int data)
{
if (root.data > data) {
MinimumElement = Math.min(root.data, MinimumElement);
}
for(Node child : root.childs) {
smallestEleUnderRange(child, data);
}
}
// Function to find the Kth smallest element
static int kthSmallestElement(Node root, int k)
{
int ans = Integer.MIN_VALUE;
for (int i = 0; i < k; i++) {
smallestEleUnderRange(root, ans);
ans = MinimumElement;
MinimumElement = Integer.MAX_VALUE;
}
return ans;
}
// Function to create a new node
static Node newNode(int data)
{
Node temp = new Node(data);
return temp;
}
public static void main(String[] args) {
/* Let us create below tree
* 10
* / / \ \
* 2 34 56 100
* / \ | / | \
* 77 88 1 7 8 9
*/
Node root = newNode(10);
(root.childs).add(newNode(2));
(root.childs).add(newNode(34));
(root.childs).add(newNode(56));
(root.childs).add(newNode(100));
(root.childs.get(0).childs).add(newNode(77));
(root.childs.get(0).childs).add(newNode(88));
(root.childs.get(2).childs).add(newNode(1));
(root.childs.get(3).childs).add(newNode(7));
(root.childs.get(3).childs).add(newNode(8));
(root.childs.get(3).childs).add(newNode(9));
System.out.print(kthSmallestElement(root, 3));
}
}
// This code is contributed by mukesh07.
Python3
# Python3 program for the above approach
import sys
# Structure of a node
class Node:
def __init__(self, data):
self.data = data
self.childs = []
# Global variable set to Maximum
MinimumElement = sys.maxsize
# Function that gives the smallest
# element under the range of key
def smallestEleUnderRange(root, data):
global MinimumElement
if root.data > data:
MinimumElement = min(root.data, MinimumElement)
for child in range(len(root.childs)):
smallestEleUnderRange(root.childs[child], data)
# Function to find the Kth smallest element
def kthSmallestElement(root, k):
global MinimumElement
ans = -sys.maxsize
for i in range(k):
smallestEleUnderRange(root, ans)
ans = MinimumElement
MinimumElement = sys.maxsize
return ans
# Function to create a new node
def newNode(data):
temp = Node(data)
return temp
""" Let us create below tree
* 10
* / / \ \
* 2 34 56 100
* / \ | / | \
* 77 88 1 7 8 9
"""
root = newNode(10)
(root.childs).append(newNode(2))
(root.childs).append(newNode(34))
(root.childs).append(newNode(56))
(root.childs).append(newNode(100))
(root.childs[0].childs).append(newNode(77))
(root.childs[0].childs).append(newNode(88))
(root.childs[2].childs).append(newNode(1))
(root.childs[3].childs).append(newNode(7))
(root.childs[3].childs).append(newNode(8))
(root.childs[3].childs).append(newNode(9))
print(kthSmallestElement(root, 3))
# This code is contributed by divyesh072019.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG {
// Class containing left and
// right child of current
// node and key value
class Node {
public int data;
public List childs;
public Node(int data)
{
this.data = data;
childs = new List();
}
}
// Global variable set to Maximum
static int MinimumElement = Int32.MaxValue;
// Function that gives the smallest
// element under the range of key
static void smallestEleUnderRange(Node root, int data)
{
if (root.data > data) {
MinimumElement = Math.Min(root.data, MinimumElement);
}
foreach(Node child in root.childs) {
smallestEleUnderRange(child, data);
}
}
// Function to find the Kth smallest element
static int kthSmallestElement(Node root, int k)
{
int ans = Int32.MinValue;
for (int i = 0; i < k; i++) {
smallestEleUnderRange(root, ans);
ans = MinimumElement;
MinimumElement = Int32.MaxValue;
}
return ans;
}
// Function to create a new node
static Node newNode(int data)
{
Node temp = new Node(data);
return temp;
}
static void Main() {
/* Let us create below tree
* 10
* / / \ \
* 2 34 56 100
* / \ | / | \
* 77 88 1 7 8 9
*/
Node root = newNode(10);
(root.childs).Add(newNode(2));
(root.childs).Add(newNode(34));
(root.childs).Add(newNode(56));
(root.childs).Add(newNode(100));
(root.childs[0].childs).Add(newNode(77));
(root.childs[0].childs).Add(newNode(88));
(root.childs[2].childs).Add(newNode(1));
(root.childs[3].childs).Add(newNode(7));
(root.childs[3].childs).Add(newNode(8));
(root.childs[3].childs).Add(newNode(9));
Console.Write(kthSmallestElement(root, 3));
}
}
// This code is contributed by divyeshrabadiya07.
Javascript
7
时间复杂度: O(N * K),其中N是给定树中的节点数。
辅助空间: O(1),但递归堆栈最多使用 O(N) 空间。