Imprima todos los Nodes excepto el Node más a la derecha de cada nivel del árbol binario

Dado un árbol binario, la tarea es imprimir todos los Nodes excepto el más a la derecha en cada nivel del árbol. La raíz se considera en el nivel 0 y el Node más a la derecha de cualquier nivel se considera como un Node en la posición 0.
Ejemplos: 
 

Input:
          1
       /     \
      2       3
    /   \       \
   4     5       6
        /  \
       7    8
      /      \
     9        10

Output:
2
4 5
7
9

Input:
          1
        /   \
       2     3
        \     \
         4     5
Output:
2
4

Enfoque: para imprimir los Nodes nivel por nivel, use el recorrido de orden de niveles. La idea se basa en el recorrido del orden de nivel de impresión línea por línea . Para eso, recorra los Nodes nivel por nivel y si el Node en la cola de orden de nivel es el último Node, ese Node será el Node más a la derecha y no imprimirá ese Node.
A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Structure of the tree node
struct Node {
    int data;
    Node *left, *right;
};
 
// Utility method to create a node
struct Node* newNode(int data)
{
    struct Node* node = new Node;
    node->data = data;
    node->left = node->right = NULL;
    return (node);
}
 
// Function to print all the nodes
// except the rightmost in every level
// of the given binary tree
// with level order traversal
void excluderightmost(Node* root)
{
    // Base Case
    if (root == NULL)
        return;
 
    // Create an empty queue for level
    // order traversal
    queue<Node*> q;
 
    // Enqueue root
    q.push(root);
 
    while (1) {
 
        // nodeCount (queue size) indicates
        // number of nodes at current level.
        int nodeCount = q.size();
        if (nodeCount == 0)
            break;
 
        // Dequeue all nodes of current level
        // and Enqueue all nodes of next level
        while (nodeCount > 0) {
            Node* node = q.front();
 
            // If node is not rightmost print
            if (nodeCount != 1)
                cout << node->data << " ";
            q.pop();
            if (node->left != NULL)
                q.push(node->left);
            if (node->right != NULL)
                q.push(node->right);
            nodeCount--;
        }
        cout << "\n";
    }
}
 
// Driver code
int main()
{
    struct Node* root = newNode(1);
    root->left = newNode(2);
    root->right = newNode(3);
    root->left->left = newNode(4);
    root->left->right = newNode(5);
    root->right->left = newNode(6);
    root->right->right = newNode(7);
    root->left->right->left = newNode(8);
    root->left->right->right = newNode(9);
    root->left->right->right->right = newNode(10);
 
    excluderightmost(root);
 
    return 0;
}

Java

// Java implementation of the approach
import java.util.*;
 
class Sol {
 
    // Structure of the tree node
    static class Node {
        int data;
        Node left, right;
    };
 
    // Utility method to create a node
    static Node newNode(int data)
    {
        Node node = new Node();
        node.data = data;
        node.left = node.right = null;
        return (node);
    }
 
    // Function to print all the nodes
    // except the rightmost in every level
    // of the given binary tree
    // with level order traversal
    static void excluderightmost(Node root)
    {
        // Base Case
        if (root == null)
            return;
 
        // Create an empty queue for level
        // order traversal
        Queue<Node> q = new LinkedList<Node>();
 
        // Enqueue root
        q.add(root);
 
        while (true) {
 
            // nodeCount (queue size) indicates
            // number of nodes at current level.
            int nodeCount = q.size();
            if (nodeCount == 0)
                break;
 
            // Dequeue all nodes of current level
            // and Enqueue all nodes of next level
            while (nodeCount > 0) {
                Node node = q.peek();
 
                // If node is not rightmost print
                if (nodeCount != 1)
                    System.out.print(node.data + " ");
                q.remove();
                if (node.left != null)
                    q.add(node.left);
                if (node.right != null)
                    q.add(node.right);
 
                nodeCount--;
            }
            System.out.println();
        }
    }
 
    // Driver code
    public static void main(String args[])
    {
        Node root = newNode(1);
        root.left = newNode(2);
        root.right = newNode(3);
        root.left.left = newNode(4);
        root.left.right = newNode(5);
        root.right.left = newNode(6);
        root.right.right = newNode(7);
        root.left.right.left = newNode(8);
        root.left.right.right = newNode(9);
        root.left.right.right.right = newNode(10);
 
        excluderightmost(root);
    }
}

Python

# Python implementation of the approach
from collections import deque
    
# Structure of the tree node
class Node:
    def __init__(self):
        self.data = 0
        self.left = None
        self.right = None
    
# Utility method to create a node
def newNode(data: int) -> Node:
    node = Node()
    node.data = data
    node.left = None
    node.right = None
    return node
    
# Function to print all the nodes
# except the rightmost in every level
# of the given binary tree
# with level order traversal
def excluderightmost(root: Node):
    
    # Base Case
    if root is None:
        return
    
    # Create an empty queue for level
    # order traversal
    q = deque()
    
    # Enqueue root
    q.append(root)
    
    while 1:
    
        # nodeCount (queue size) indicates
        # number of nodes at current level
        nodeCount = len(q)
        if nodeCount == 0:
            break
    
        # Dequeue all nodes of current level
        # and Enqueue all nodes of next level
        while nodeCount > 0:
            node = q[0]
    
            # If Node is not right most print
            if nodeCount != 1:
                print(node.data, end =" ")
            q.popleft()
              
            if node.left is not None:
                q.append(node.left)
            if node.right is not None:
                q.append(node.right)
              
            nodeCount -= 1
        print()
    
# Driver Code
if __name__ == "__main__":
    root = Node()
    root = newNode(1)
    root.left = newNode(2)
    root.right = newNode(3)
    root.left.left = newNode(4)
    root.left.right = newNode(5)
    root.right.left = newNode(6)
    root.right.right = newNode(7)
    root.left.right.left = newNode(8)
    root.left.right.right = newNode(9)
    root.left.right.right.right = newNode(10)
    excluderightmost(root)

C#

// C# implementation of the above approach
using System;
using System.Collections.Generic;
 
class GFG {
 
    // Structure of the tree node
    public class Node {
        public int data;
        public Node left, right;
    };
 
    // Utility method to create a node
    static Node newNode(int data)
    {
        Node node = new Node();
        node.data = data;
        node.left = node.right = null;
        return (node);
    }
 
    // Function to print all the nodes
    // except the rightmost in every level
    // of the given binary tree
    // with level order traversal
    static void excluderightmost(Node root)
    {
        // Base Case
        if (root == null)
            return;
 
        // Create an empty queue for level
        // order traversal
        Queue<Node> q = new Queue<Node>();
 
        // Enqueue root
        q.Enqueue(root);
 
        while (true) {
 
            // nodeCount (queue size) indicates
            // number of nodes at current level.
            int nodeCount = q.Count;
            if (nodeCount == 0)
                break;
 
            // Dequeue all nodes of current level
            // and Enqueue all nodes of next level
            while (nodeCount > 0) {
                Node node = q.Peek();
 
                // if Node is not right most print
                if (nodeCount != 1)
                    Console.Write(node.data + " ");
                q.Dequeue();
                if (node.left != null)
                    q.Enqueue(node.left);
                if (node.right != null)
                    q.Enqueue(node.right);
 
                nodeCount--;
            }
            Console.WriteLine();
        }
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        Node root = newNode(1);
        root.left = newNode(2);
        root.right = newNode(3);
        root.left.left = newNode(4);
        root.left.right = newNode(5);
        root.right.left = newNode(6);
        root.right.right = newNode(7);
        root.left.right.left = newNode(8);
        root.left.right.right = newNode(9);
        root.left.right.right.right = newNode(10);
 
        excluderightmost(root);
    }
}

Javascript

<script>
  
// JavaScript implementation of the above approach
// Structure of the tree node
 
class Node {
 
    constructor()
    {
        this.data = 0;
        this.left = null;
        this.right = null;
    }
};
// Utility method to create a node
function newNode(data)
{
    var node = new Node();
    node.data = data;
    node.left = node.right = null;
    return (node);
}
// Function to print all the nodes
// except the rightmost in every level
// of the given binary tree
// with level order traversal
function excluderightmost(root)
{
    // Base Case
    if (root == null)
        return;
    // Create an empty queue for level
    // order traversal
    var q = [];
    // push root
    q.push(root);
    while (true) {
        // nodeCount (queue size) indicates
        // number of nodes at current level.
        var nodeCount = q.length;
        if (nodeCount == 0)
            break;
        // Dequeue all nodes of current level
        // and push all nodes of next level
        while (nodeCount > 0) {
            var node = q[0];
            // if Node is not right most print
            if (nodeCount != 1)
                document.write(node.data + " ");
            q.shift();
            if (node.left != null)
                q.push(node.left);
            if (node.right != null)
                q.push(node.right);
            nodeCount--;
        }
        document.write("<br>");
    }
}
// Driver code
var root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.left = newNode(4);
root.left.right = newNode(5);
root.right.left = newNode(6);
root.right.right = newNode(7);
root.left.right.left = newNode(8);
root.left.right.right = newNode(9);
root.left.right.right.right = newNode(10);
excluderightmost(root);
 
</script>
Producción: 

2 
4 5 6 
8

 

Publicación traducida automáticamente

Artículo escrito por SHUBHAMSINGH10 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 *