📌  相关文章
📜  从给定的二叉树中找到K个最小的叶子节点

📅  最后修改于: 2021-05-17 16:08:38             🧑  作者: Mango

给定一个二叉树和一个整数K ,任务是从给定的二叉树中找到K个最小的叶子节点。叶节点的数量将始终至少为K。

例子:

方法:请按照以下步骤解决问题:

  • 遍历二叉树。
  • 检查每个节点是否既不包含左子代也不包含右子代。如果发现为真,则该节点为叶节点。将所有此类节点存储在数组中。
  • 对叶节点数组进行排序,并从该数组中打印出K个最小的叶子值。

下面是上述方法的实现:

C++
// C++ program of the
// above approach
 
#include 
using namespace std;
 
// Structure of
// binary tree node
struct Node {
    int data;
    Node *left, *right;
};
 
// Function to create new node
Node* newNode(int data)
{
    Node* temp = new Node();
    temp->data = data;
    temp->left = temp->right = NULL;
    return temp;
}
 
// Utility function which calculates
// smallest three nodes of all leaf nodes
void storeLeaf(Node* root, vector& arr)
{
    if (!root)
        return;
 
    // Check if current root is a leaf node
    if (!root->left and !root->right) {
        arr.push_back(root->data);
        return;
    }
 
    // Traverse the left
    // and right subtree
    storeLeaf(root->left, arr);
    storeLeaf(root->right, arr);
}
 
// Function to find the K smallest
// nodes of the Binary Tree
void KSmallest(Node* root, int k)
{
    vector arr;
    storeLeaf(root, arr);
 
    // Sorting the Leaf nodes array
    sort(arr.begin(), arr.end());
 
    // Loop to print the K smallest
    // Leaf nodes of the array
    for (int i = 0; i < k; i++) {
        if (i < arr.size()) {
            cout << arr[i] << " ";
        }
        else {
            break;
        }
    }
}
 
// Driver Code
int main()
{
    // Construct binary tree
    Node* root = newNode(1);
    root->left = newNode(2);
    root->left->left = newNode(4);
    root->left->left->left = newNode(21);
    root->left->right = newNode(5);
    root->left->right->right = newNode(8);
    root->right = newNode(3);
    root->right->left = newNode(6);
    root->right->right = newNode(7);
    root->right->right->right = newNode(19);
 
    // Function Call
    KSmallest(root, 3);
    return 0;
}


Java
// Java program of the
// above approach
import java.util.*;
 
class GFG{
 
// Structure of
// binary tree node
static class Node
{
    int data;
    Node left, right;
};
 
// Function to create new node
static Node newNode(int data)
{
    Node temp = new Node();
    temp.data = data;
    temp.left = temp.right = null;
    return temp;
}
 
// Utility function which calculates
// smallest three nodes of all leaf nodes
static Vector storeLeaf(Node root,
                       Vector arr)
{
    if (root == null)
        return arr;
 
    // Check if current root is a leaf node
    if (root.left == null &&
       root.right == null)
    {
        arr.add(root.data);
        return arr;
    }
 
    // Traverse the left
    // and right subtree
    arr = storeLeaf(root.left, arr);
    arr = storeLeaf(root.right, arr);
    return arr;
}
 
// Function to find the K smallest
// nodes of the Binary Tree
static void KSmallest(Node root, int k)
{
    Vector arr = new Vector();
    arr = storeLeaf(root, arr);
 
    // Sorting the Leaf nodes array
    Collections.sort(arr);
 
    // Loop to print the K smallest
    // Leaf nodes of the array
    for(int i = 0; i < k; i++)
    {
        if (i < arr.size())
        {
            System.out.print(arr.get(i) + " ");
        }
        else
        {
            break;
        }
    }
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Construct binary tree
    Node root = newNode(1);
    root.left = newNode(2);
    root.left.left = newNode(4);
    root.left.left.left = newNode(21);
    root.left.right = newNode(5);
    root.left.right.right = newNode(8);
    root.right = newNode(3);
    root.right.left = newNode(6);
    root.right.right = newNode(7);
    root.right.right.right = newNode(19);
 
    // Function call
    KSmallest(root, 3);
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program for the
# above approach
 
# Binary tree node
class Node:
    def __init__(self, data):
       
        self.data = data
        self.left = None
        self.right = None
 
# Utility function which calculates
# smallest three nodes of all leaf nodes
def storeLeaf(root: Node,
              arr : list) -> None:
 
    if (not root):
        return
 
    # Check if current root
    # is a leaf node
    if (not root.left and
        not root.right):
        arr.append(root.data)
        return
 
    # Traverse the left
    # and right subtree
    storeLeaf(root.left, arr)
    storeLeaf(root.right, arr)
 
# Function to find the K smallest
# nodes of the Binary Tree
def KSmallest(root: Node,
              k: int) -> None:
 
    arr = []
    storeLeaf(root, arr)
 
    # Sorting the Leaf
    # nodes array
    arr.sort()
 
    # Loop to print the K smallest
    # Leaf nodes of the array
    for i in range(k):
        if (i < len(arr)):
            print(arr[i], end = " ")
        else:
            break
 
# Driver Code
if __name__ == "__main__":
 
    # Construct binary tree
    root = Node(1)
    root.left = Node(2)
    root.left.left = Node(4)
    root.left.left.left = Node(21)
    root.left.right = Node(5)
    root.left.right.right = Node(8)
    root.right = Node(3)
    root.right.left = Node(6)
    root.right.right = Node(7)
    root.right.right.right = Node(19)
 
    # Function Call
    KSmallest(root, 3)
 
# This code is contributed by sanjeev2552


C#
// C# program of the
// above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Structure of
// binary tree node
class Node
{
    public int data;
    public Node left, right;
};
 
// Function to create new node
static Node newNode(int data)
{
    Node temp = new Node();
    temp.data = data;
    temp.left = temp.right = null;
    return temp;
}
 
// Utility function which calculates
// smallest three nodes of all leaf nodes
static List storeLeaf(Node root,
                           List arr)
{
    if (root == null)
        return arr;
 
    // Check if current root is a leaf node
    if (root.left == null &&
       root.right == null)
    {
        arr.Add(root.data);
        return arr;
    }
 
    // Traverse the left
    // and right subtree
    arr = storeLeaf(root.left, arr);
    arr = storeLeaf(root.right, arr);
    return arr;
}
 
// Function to find the K smallest
// nodes of the Binary Tree
static void KSmallest(Node root, int k)
{
    List arr = new List();
    arr = storeLeaf(root, arr);
 
    // Sorting the Leaf nodes array
    arr.Sort();
 
    // Loop to print the K smallest
    // Leaf nodes of the array
    for(int i = 0; i < k; i++)
    {
        if (i < arr.Count)
        {
            Console.Write(arr[i] + " ");
        }
        else
        {
            break;
        }
    }
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Construct binary tree
    Node root = newNode(1);
    root.left = newNode(2);
    root.left.left = newNode(4);
    root.left.left.left = newNode(21);
    root.left.right = newNode(5);
    root.left.right.right = newNode(8);
    root.right = newNode(3);
    root.right.left = newNode(6);
    root.right.right = newNode(7);
    root.right.right.right = newNode(19);
 
    // Function call
    KSmallest(root, 3);
}
}
 
// This code is contributed by Amit Katiyar


输出:
6 8 19



时间复杂度: O(N + L * logL),这里L是叶节点数,N是节点数。
辅助空间: O(L + logN)