📌  相关文章
📜  打印具有 K 个叶子的二叉树中的所有节点

📅  最后修改于: 2022-05-13 01:57:20.530000             🧑  作者: Mango

打印具有 K 个叶子的二叉树中的所有节点

给定一棵二叉树和一个整数值 K,任务是找到给定二叉树中的所有节点,在以它们为根的子树中具有 K 个叶子。

例子 :

// For above binary tree
Input : k = 2
Output: {3}
// here node 3 have k = 2 leaves

Input : k = 1
Output: {6}
// here node 6 have k = 1 leave

这里任何具有 K 个叶子的节点都意味着左子树和右子树中的叶子之和必须等于 K。所以为了解决这个问题,我们使用树的后序遍历。首先我们在左子树中计算叶子,然后在右子树中计算叶子,如果总和等于 K,则打印当前节点。在每个递归调用中,我们将左子树和右子树的叶子总和返回给它的祖先。
以下是上述方法的实现:

C++
// C++ program to count all nodes having k leaves
// in subtree rooted with them
#include
using namespace std;
 
/* A binary tree node  */
struct Node
{
    int data ;
    struct Node * left, * right ;
};
 
/* Helper function that allocates a new node with the
   given data and NULL left and right pointers. */
struct Node * newNode(int data)
{
    struct Node * node = new Node;
    node->data = data;
    node->left = node->right = NULL;
    return (node);
}
 
// Function to print all nodes having k leaves
int kLeaves(struct Node *ptr,int k)
{
    // Base Conditions : No leaves
    if (ptr == NULL)
        return 0;
 
    // if node is leaf
    if (ptr->left == NULL && ptr->right == NULL)
        return 1;
 
    // total leaves in subtree rooted with this
    // node
    int total = kLeaves(ptr->left, k) +
                kLeaves(ptr->right, k);
 
    // Print this node if total is k
    if (k == total)
        cout << ptr->data << " ";
 
    return total;
}
 
// Driver program to run the case
int main()
{
    struct Node *root = newNode(1);
    root->left        = newNode(2);
    root->right       = newNode(4);
    root->left->left  = newNode(5);
    root->left->right = newNode(6);
    root->left->left->left  = newNode(9);
    root->left->left->right  = newNode(10);
    root->right->right = newNode(8);
    root->right->left  = newNode(7);
    root->right->left->left  = newNode(11);
    root->right->left->right  = newNode(12);
 
    kLeaves(root, 2);
 
    return 0;
}


Java
// Java program to count all nodes having k leaves
// in subtree rooted with them
class GfG {
 
/* A binary tree node */
static class Node
{
    int data ;
    Node left, right ;
    Node(int data)
    {
        this.data = data;
    }
    Node()
    {
         
    }
}
 
/* Helper function that allocates a new node with the
given data and NULL left and right pointers. */
static Node newNode(int data)
{
    Node node = new Node();
    node.data = data;
    node.left = null;
    node.right = null;
    return (node);
}
 
// Function to print all nodes having k leaves
static int kLeaves(Node ptr,int k)
{
    // Base Conditions : No leaves
    if (ptr == null)
        return 0;
 
    // if node is leaf
    if (ptr.left == null && ptr.right == null)
        return 1;
 
    // total leaves in subtree rooted with this
    // node
    int total = kLeaves(ptr.left, k) + kLeaves(ptr.right, k);
 
    // Print this node if total is k
    if (k == total)
        System.out.print(ptr.data + " ");
 
    return total;
}
 
// Driver program to run the case
public static void main(String[] args)
{
    Node root = newNode(1);
    root.left     = newNode(2);
    root.right     = newNode(4);
    root.left.left = newNode(5);
    root.left.right = newNode(6);
    root.left.left.left = newNode(9);
    root.left.left.right = newNode(10);
    root.right.right = newNode(8);
    root.right.left = newNode(7);
    root.right.left.left = newNode(11);
    root.right.left.right = newNode(12);
 
    kLeaves(root, 2);
 
}
}


Python3
# Python3 program to count all nodes 
# having k leaves in subtree rooted with them
 
# A binary tree node has data, pointer to
# left child and a pointer to right child
# Helper function that allocates a new node 
# with the given data and None left and
# right pointers
class newNode:
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None
 
# Function to print all nodes having k leaves
def kLeaves(ptr, k):
 
    # Base Conditions : No leaves
    if (ptr == None):
        return 0
 
    # if node is leaf
    if (ptr.left == None and
        ptr.right == None):
        return 1
 
    # total leaves in subtree rooted with this
    # node
    total = kLeaves(ptr.left, k) + \
            kLeaves(ptr.right, k)
 
    # Print this node if total is k
    if (k == total):
        print(ptr.data, end = " ")
 
    return total
 
# Driver code
root = newNode(1)
root.left = newNode(2)
root.right = newNode(4)
root.left.left = newNode(5)
root.left.right = newNode(6)
root.left.left.left = newNode(9)
root.left.left.right = newNode(10)
root.right.right = newNode(8)
root.right.left = newNode(7)
root.right.left.left = newNode(11)
root.right.left.right = newNode(12)
 
kLeaves(root, 2)
 
# This code is contributed by SHUBHAMSINGH10


C#
// C# program to count all nodes having k leaves
// in subtree rooted with them
using System;
     
class GfG
{
 
/* A binary tree node */
public class Node
{
    public int data ;
    public Node left, right ;
    public Node(int data)
    {
        this.data = data;
    }
    public Node()
    {
         
    }
}
 
/* Helper function that allocates a new node with the
given data and NULL left and right pointers. */
static Node newNode(int data)
{
    Node node = new Node();
    node.data = data;
    node.left = null;
    node.right = null;
    return (node);
}
 
// Function to print all nodes having k leaves
static int kLeaves(Node ptr,int k)
{
    // Base Conditions : No leaves
    if (ptr == null)
        return 0;
 
    // if node is leaf
    if (ptr.left == null && ptr.right == null)
        return 1;
 
    // total leaves in subtree rooted with this
    // node
    int total = kLeaves(ptr.left, k) + kLeaves(ptr.right, k);
 
    // Print this node if total is k
    if (k == total)
        Console.Write(ptr.data + " ");
 
    return total;
}
 
// Driver program to run the case
public static void Main(String[] args)
{
    Node root = newNode(1);
    root.left = newNode(2);
    root.right = newNode(4);
    root.left.left = newNode(5);
    root.left.right = newNode(6);
    root.left.left.left = newNode(9);
    root.left.left.right = newNode(10);
    root.right.right = newNode(8);
    root.right.left = newNode(7);
    root.right.left.left = newNode(11);
    root.right.left.right = newNode(12);
 
    kLeaves(root, 2);
 
}
}
 
// This code has been contributed by 29AjayKumar


Javascript


输出:

5 7

时间复杂度: O(n)