Home30 Days Of CodeHackerRank Day 23 : BST Level order traversal 30 days of code...

HackerRank Day 23 : BST Level order traversal 30 days of code solution

Today we are going to solve HackerRank Day 23 : BST Level order traversal 30 days of code solution in CC++, Java, Python & Javascript.

Objective

Today, we’re going further with Binary Search Trees.

Task

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.

Hint: You’ll find a queue helpful in completing this challenge.

Function Description

Complete the levelOrder function in the editor below.

levelOrder has the following parameter:
– Node pointer root: a reference to the root of the tree

Prints
– Print node.data items as space-separated line of integers. No return value is expected.

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, T (the number of test cases).
The T subsequent lines each contain an integer, data, denoting the value of an element that must be added to the BST.

Constraints

  • 1 <= N <= 20
  • 1 <= node.data[i] <= 100

Output Format

Print the data value of each node in the tree’s level-order traversal as a single line of N space-separated integers.

Sample Input

6
3
5
4
7
2
1

Sample Output

3 2 5 1 4 7 

Explanation

We traverse each level of the tree from the root downward, and we process the nodes at each level from left to right. The resulting level-order traversal is 3 = 2 = 5 = 1 = 4 = 7, and we print these data values as a single line of space-separated integers.


HackerRank Day 23 : BST Level order traversal 30 days of code solution

BST Level order traversal HackerRank Solution in C

#define max(a, b) (a > b ? a : b)

int getHeight(Node *root) {
    if (root == NULL)
        return 0;
    else
        return 1 + max(getHeight(root->left), getHeight(root->right));
}

void printGivenLevel(Node *root, int level) {
    if (root == NULL)
        return;
    if (level == 1)
        printf("%d ", root->data);
    else if (level > 1)
    {
        printGivenLevel(root->left, level-1);
        printGivenLevel(root->right, level-1);
    } 
}

void levelOrder(Node* root){
  //Write your code here
    int height = getHeight(root);
    int i;
    for (i = 1; i <= height; i++) {
        printGivenLevel(root, i);
    }
}

BST Level order traversal HackerRank Solution in C++

void levelOrder(Node * root){
        std::queue<Node*> q;
        Node* c;
  
        if (root != NULL) {
            q.push(root);
        }
        
        while (!q.empty()) {
            c = q.front();
            q.pop();
            cout << c->data << " ";
            if (c->left!=NULL) q.push(c->left);
            if (c->right!=NULL) q.push(c->right);
        }
	}

BST Level order traversal HackerRank Solution in Java

static LinkedList<Integer> queue = new LinkedList();
static void levelOrder(Node root){
    LinkedList<Node> treeQueue = new LinkedList();
    treeQueue.add(root);
    while(treeQueue.peek() != null) {
        Node toprint = treeQueue.remove();
        System.out.print(toprint.data);
        if(toprint.left != null) {
            treeQueue.add(toprint.left);
        }
        if(toprint.right != null) {
            treeQueue.add(toprint.right);
        }
        if(treeQueue.peek() != null) {
            System.out.print(" ");
        }
    }
    
    }

Binary Search Trees HackerRank Solution in Python 3

 def levelOrder(self,root):
        if root is None:
            return 
        
        qu = []
        qu.append(root)

        while len(qu) !=0:
            p = qu.pop(0)
            print(p.data, end=' ')
            if p.left is not None:
                qu.append(p.left)
            if p.right is not None:
                qu.append(p.right)
        

Binary Search Trees HackerRank Solution in JavaScript

 var queue = [root];
      while (queue.length > 0) {
        var node = queue.shift();
        write(node.data + " ");
        if(node.left) {
         queue.push(node.left);
        }
        if (node.right) {
         queue.push(node.right);
        }
      }
      function write(str){
        process.stdout.write(str);
      }

NEXT: HackerRank Day 24: More Linked Lists 30 days of code solution

30 Days of Code HackerRank Solutions List – Day 0 to Day 29

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

- Advertisment -

Categories