Imprimir Nodes de hoja en un nivel dado

Dado un árbol binario, imprima todos los Nodes hoja de un árbol binario en un nivel L dado.
Ejemplos:

Input:    
           1
          /  \
         2    3
        /    /  \
       4    5    6
 level = 3

Output: 4 5 6

Input:    
             7
            /  \
           2    3
          / \    \
         4   9   10
                 /
                6
   level = 3

Output: 4 9

Enfoque : Atraviese recursivamente el árbol en orden de nivel. Si el nivel actual es el mismo que el nivel dado, compruebe si el Node actual es un Node hoja o no. Si es un Node de hoja, imprímalo.
A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ program to print all the
// leaf nodes at a given level
// in a Binary tree
#include <bits/stdc++.h>
using namespace std;
 
// Binary tree node
struct node {
    struct node* left;
    struct node* right;
    int data;
};
 
// Function to create a new
// Binary node
struct node* newNode(int data)
{
    struct node* temp = new node;
 
    temp->data = data;
    temp->left = NULL;
    temp->right = NULL;
 
    return temp;
}
 
// Function to print Leaf Nodes at
// a given level
void PrintLeafNodes(struct node* root, int level)
{
    if (root == NULL)
        return;
 
    if (level == 1) {
        if (root->left == NULL && root->right == NULL)
            cout << root->data << " ";
    }
    else if (level > 1) {
        PrintLeafNodes(root->left, level - 1);
        PrintLeafNodes(root->right, level - 1);
    }
}
 
// Driver code
int main()
{
    struct node* root = newNode(1);
    root->left = newNode(2);
    root->right = newNode(3);
    root->left->left = newNode(6);
    root->right->right = newNode(4);
    root->left->left->left = newNode(8);
    root->left->left->right = newNode(7);
 
    int level = 4;
 
    PrintLeafNodes(root, level);
 
    return 0;
}

Java

// Java program to print all the
// leaf nodes at a given level
// in a Binary tree
class GFG
{
 
    // Binary tree node
    static class node
    {
 
        node left;
        node right;
        int data;
    };
 
    // Function to create a new
    // Binary node
    static node newNode(int data)
    {
        node temp = new node();
 
        temp.data = data;
        temp.left = null;
        temp.right = null;
 
        return temp;
    }
 
    // Function to print Leaf Nodes at
    // a given level
    static void PrintLeafNodes(node root, int level)
    {
        if (root == null)
        {
            return;
        }
 
        if (level == 1)
        {
            if (root.left == null && root.right == null)
            {
                System.out.print(root.data + " ");
            }
 
        }
        else if (level > 1)
        {
            PrintLeafNodes(root.left, level - 1);
            PrintLeafNodes(root.right, level - 1);
        }
    }
 
    // Driver code
    public static void main(String[] args)
    {
        node root = newNode(1);
        root.left = newNode(2);
        root.right = newNode(3);
        root.left.left = newNode(6);
        root.right.right = newNode(4);
        root.left.left.left = newNode(8);
        root.left.left.right = newNode(7);
 
        int level = 4;
 
        PrintLeafNodes(root, level);
    }
}
 
// This code contributed by Rajput-Ji

Python3

# Python3 program to print all the
# leaf nodes at a given level
# in a Binary tree
 
# Binary tree node
class node:
     
    def __init__(self, data):
        self.left=None
        self.right=None
        self.data=data
  
# Function to create a new
# Binary node
def newNode(data):
    return node(data)
  
# Function to print Leaf Nodes at
# a given level
def PrintLeafNodes(root, level):
    if (root == None):
        return
  
    if (level == 1):
        if (root.left == None and
            root.right == None):
            print(root.data, end = " ")
     
    elif (level > 1):
        PrintLeafNodes(root.left, level - 1)
        PrintLeafNodes(root.right, level - 1)
         
if __name__=="__main__":
     
    root=newNode(1)
    root.left = newNode(2);
    root.right = newNode(3);
    root.left.left = newNode(6);
    root.right.right = newNode(4);
    root.left.left.left = newNode(8);
    root.left.left.right = newNode(7);
    level = 4;
    PrintLeafNodes(root, level);
 
# This code is contributed by rutvik_56

C#

// C# program to print all the
// leaf nodes at a given level
// in a Binary tree
using System;
 
class GFG
{
 
    // Binary tree node
    public class node
    {
 
        public node left;
        public node right;
        public int data;
    };
 
    // Function to create a new
    // Binary node
    static node newNode(int data)
    {
        node temp = new node();
 
        temp.data = data;
        temp.left = null;
        temp.right = null;
 
        return temp;
    }
 
    // Function to print Leaf Nodes at
    // a given level
    static void PrintLeafNodes(node root, int level)
    {
        if (root == null)
        {
            return;
        }
 
        if (level == 1)
        {
            if (root.left == null && root.right == null)
            {
                Console.Write(root.data + " ");
            }
 
        }
        else if (level > 1)
        {
            PrintLeafNodes(root.left, level - 1);
            PrintLeafNodes(root.right, level - 1);
        }
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        node root = newNode(1);
        root.left = newNode(2);
        root.right = newNode(3);
        root.left.left = newNode(6);
        root.right.right = newNode(4);
        root.left.left.left = newNode(8);
        root.left.left.right = newNode(7);
 
        int level = 4;
 
        PrintLeafNodes(root, level);
    }
}
 
// This code has been contributed by 29AjayKumar

Javascript

<script>
 
    // JavaScript program to print all the
    // leaf nodes at a given level
    // in a Binary tree
     
    // Binary tree node
    class node
    {
        constructor(data) {
           this.left = null;
           this.right = null;
           this.data = data;
        }
    }
     
    // Function to create a new
    // Binary node
    function newNode(data)
    {
        let temp = new node(data);
  
        temp.data = data;
        temp.left = null;
        temp.right = null;
  
        return temp;
    }
  
    // Function to print Leaf Nodes at
    // a given level
    function PrintLeafNodes(root, level)
    {
        if (root == null)
        {
            return;
        }
  
        if (level == 1)
        {
            if (root.left == null && root.right == null)
            {
                document.write(root.data + " ");
            }
  
        }
        else if (level > 1)
        {
            PrintLeafNodes(root.left, level - 1);
            PrintLeafNodes(root.right, level - 1);
        }
    }
     
    let root = newNode(1);
    root.left = newNode(2);
    root.right = newNode(3);
    root.left.left = newNode(6);
    root.right.right = newNode(4);
    root.left.left.left = newNode(8);
    root.left.left.right = newNode(7);
 
    let level = 4;
 
    PrintLeafNodes(root, level);
 
</script>
Producción: 

8 7

 

Complejidad de tiempo : O (N) donde N es el número de Nodes en un árbol binario.
Espacio Auxiliar: O(N) 

Publicación traducida automáticamente

Artículo escrito por Sakshi_Srivastava y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *