In this HackerRank Day 23: BST Level-Order Traversal problem solution A level-order traversal, also known as a breadth-first search, visits each level of a tree's nodes from left to right, top to bottom. You are given a pointer, root, pointing to the root of a binary search tree. Complete the levelOrder function provided in your editor so that it prints the level-order traversal of the binary search tree.

HackerRank Day 23: BST Level-Order Traversal problem solution

Problem solution in Python programming.

    def levelOrder(self,root):
        ll = []
        if(root == None):
            return
        ll.append(root)
        while(len(ll) != 0):
            cur = ll[len(ll)-1]
            if(cur != None):
                print(str(cur.data), end= " ")
                ll = [cur.left] + ll
                ll = [cur.right] + ll
            ll.pop()



Problem solution in Java programming.

	static void levelOrder(Node root){
      //Write your code here
        Queue q = new LinkedList();
        if(root != null){
            System.out.print(root.data+" ");
            q.offer(root);
        }
        while(! q.isEmpty()){
            Node temp = (Node)q.poll();
            if(temp.left != null){
                System.out.print(temp.left.data+" ");
                q.offer(temp.left);
            }
            if(temp.right != null){
                System.out.print(temp.right.data+" ");
                q.offer(temp.right);
            }
        }
    }




Problem solution in C++ programming.

    void levelOrder(Node * root){
        queue<Node*> myQueue;
        
        myQueue.push(root);
        
        while (!myQueue.empty()) {
            Node *node = myQueue.front();
            myQueue.pop();
            
            cout << node->data << ' ';
            
            if (node->left) {
                myQueue.push(node->left);
            }
            
            if (node->right) {
                myQueue.push(node->right);
            }
        }
	}



Problem solution in C programming.

#include <stdbool.h>
#define MAX 255

Node * queueArray[MAX];
int front = 0;
int rear = -1;
int itemCount = 0;

Node * peek(){
    return queueArray[front];
}

bool isEmpty(){
    return itemCount == 0;
}

bool isFull(){
    return itemCount == MAX;
}

int size(){
    return itemCount;
}

void enqueue(Node * node){

    if(!isFull()){

        if(rear == MAX-1){
            rear = -1;
        }

        queueArray[++rear] = node;
        itemCount++;
    }
}

Node * dequeue(){
    Node * node = queueArray[front++];

    if(front == MAX){
        front = 0;
    }
    
    itemCount--;
    return node;  
}

void levelOrder(Node *root) {
    if(root) {
        enqueue(root);
        while(!isEmpty()) {
            Node *t = dequeue();
            printf("%i ", t->data);
            if (t->left) {
                enqueue(t->left);
            }
            if (t->right) {
                enqueue(t->right);
            }
        }
    }
}




Problem solution in Javascript programming.

        // Add your code here
        // To print values separated by spaces use process.stdout.write(someValue + ' ')
        var arr = [root];
        while(arr.length != 0) {
            var new_arr = [];
            for(var node of arr){
                process.stdout.write(node.data + ' ');
                if(node.left)
                    new_arr.push(node.left);
                if(node.right)
                    new_arr.push(node.right);
            }
            arr = new_arr;
        }