Input Format
The locked stub code in your editor reads the following inputs and assembles them into a BST:
The first line contains an integer,  (the number of test cases).
The  subsequent lines each contain an integer, , denoting the value of an element that must be added to the BST.
Output Format
Print the  value of each node in the tree's level-order traversal as a single line of  space-separated integers.
BST level order traveral problem solution 30 days of code HackerRank

Problem solution in C++ programming language.

deque<Node*> treeQueue;

void levelOrder(Node* root){
    if( root ){
        treeQueue.push_back(root);
    }
    while( !treeQueue.empty() ){  
        Node* tree = treeQueue.front();
        treeQueue.pop_front();

        cout << tree->data << " ";

        if( tree->left ){
            treeQueue.push_back( tree->left );
        }
        if( tree->right ){
            treeQueue.push_back( tree->right );
        }
    }
}

Problem solution in Java programming language.

static Queue<Node> queue = new LinkedList<Node>();

static void levelOrder(Node root){
    if( root != null ){
        queue.add(root);
    }
    while( !queue.isEmpty() ){
        Node tree = queue.remove();
        System.out.print(tree.data + " ");

        if( tree.left != null ){
            queue.add( tree.left );
        }
        if( tree.right != null ){
            queue.add( tree.right );
        }
    }
}

Problem solution in Python programming language.

def levelOrder(self, root):
        traversal = ''
        
        if root != None:
            queue = [root]
            while len(queue) != 0:
                traversal += str(queue[0].data) + ' '
                if queue[0].left != None:
                    queue.append(queue[0].left)
                if queue[0].right != None:
                    queue.append(queue[0].right)
                queue.pop(0)
        print(traversal)

Problem solution in Ruby programming language.

 def levelOrder(root)
        if root != nil
            queue = Queue.new
            queue << root
            while !queue.empty?
                tree = queue.pop
                print(tree.data, ' ')
                if !tree.left.nil?
                    queue.push(tree.left)
                end
                if !tree.right.nil?
                    queue.push(tree.right)
                end
            end
        end
    end
end

Problem solution in the Swift programming language.

func levelOrder(root: Node?) -> Void {
     guard let currentRoot = root else {
            return
        }
    
        var queue = [currentRoot]
        var traversed = [Int]()
    
        while queue.count > 0 {
            let node = queue.removeFirst()
            
            traversed.append(node.data)
            
            if let leftNode = node.left {
                queue.append(leftNode)
            }
            
            if let rightNode = node.right {
                queue.append(rightNode)
            }
        }
    
        print(traversed.map{String($0)}.joined(separator: " "))
    }