Knowee
Questions
Features
Study Tools

Question3Max. score: 100.00Level Order of TreeGiven an array of unique elements, construct a Binary Search Tree and print the Level Order of the tree.Input FormatFirst line of each test case contains N - number of nodes in the BST. The next line contains N unique integers - value of the nodes.ConstraintsPrint the Level Order of the Binary Search Tree, separate each level by newline.Output Format1 <= N <= 10000 <= ar[i] <= 10000Sample input6 10 5 15 2 7 12Sample output10 5 15 2 7 12

Question

Question3Max. score: 100.00Level Order of TreeGiven an array of unique elements, construct a Binary Search Tree and print the Level Order of the tree.Input FormatFirst line of each test case contains N - number of nodes in the BST. The next line contains N unique integers - value of the nodes.ConstraintsPrint the Level Order of the Binary Search Tree, separate each level by newline.Output Format1 <= N <= 10000 <= ar[i] <= 10000Sample input6 10 5 15 2 7 12Sample output10 5 15 2 7 12

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution 1

The question is asking to implement a Binary Search Tree (BST) from an array of unique elements and then print the level order traversal of the tree. Here are the steps to solve this problem:

Solution 2

The problem is asking to construct a Binary Search Tree (BST) from a given array of unique elements and then print the level order traversal of the tree.

Here are the steps to solve this problem:

  1. First, we need to construct the BST from the given array. We start by initializing the root of the tree with the first element of the array. Then for each subsequent element in the array, we insert it into the BST. To insert an element, we start from the root and if the element is less than the root, we go to the left child, otherwise we go to the right child. We continue this process until we find an empty spot where we can insert the new element.

  2. Once we have constructed the BST, we need to print the level order traversal of the tree. We can do this by using a queue data structure. We start by pushing the root of the tree into the queue. Then while the queue is not empty, we remove the front element from the queue, print it, and then push its left and right children (if they exist) into the queue. We continue this process until the queue is empty.

  3. The level order traversal of the tree is the sequence of elements printed in step 2. Each level is separated by a newline.

Here is a Python solution for the problem:

from collections import deque

class Node:
    def __init__(self, key):
        self.left = None
        self.right = None
        self.val = key

def insert(root, key):
    if root is None:
        return Node(key)
    else:
        if root.val < key:
            root.right = insert(root.right, key)
        else:
            root.left = insert(root.left, key)
    return root

def printLevelOrder(root):
    if root is None:
        return
    queue = deque()
    queue.append(root)
    while queue:
        nodeCount = len(queue)
        while nodeCount > 0:
            node = queue.popleft()
            print(node.val, end=' ')
            if node.left is not None:
                queue.append(node.left)
            if node.right is not None:
                queue.append(node.right)
            nodeCount -= 1
        print()

n = int(input())
arr = list(map(int, input().split()))
root = None
for i in range(n):
    root = insert(root, arr[i])
printLevelOrder(root)

This solution reads the number of nodes and the nodes' values from the input, constructs the BST, and then prints the level order traversal of the tree.

This problem has been solved

Similar Questions

Level Order of TreeGiven an array of unique elements, construct a Binary Search Tree and print the Level Order of the tree.Input FormatFirst line of each test case contains N - number of nodes in the BST. The next line contains N unique integers - value of the nodes.ConstraintsPrint the Level Order of the Binary Search Tree, separate each level by newline.Output Format1 <= N <= 10000 <= ar[i] <= 10000Sample input6 10 5 15 2 7 12Sample output10 5 15 2 7 12

Question4Max. score: 100.00Find Minimum and Maximum Values of BSTFind Minimum and MaximumWrite a relevant code snippet to print the maximum and minimum values of  the  binary search tree Sample Input40 20 60 10 30 50 70 5 15 25 35 45 55 65 75 -1 Sample Output:Minimum is : 5Maximum is : 75Sample input40 20 60 10 30 50 70 5 15 25 35 45 55 65 75 -1Sample output

Right View of TreeGiven an array of unique elements, construct a Binary Search Tree and print the right-view of the tree. Right view of a Tree is the set of nodes visible when tree is viewed from right side.Input FormatFirst line of each test case contains N - number of nodes in the BST. The next line contains N unique integers - value of the nodes.Constraints1 <= N <= 10000 <= ar[i] <= 10000Output FormatFor each test case, print the right-view of the Binary Search Tree, separated by newline.Sample input7 5 3 8 2 4 7 9Sample output5 8 9

Question6Max. score: 100.00Delete a node in BSTWrite a relevant code snippet to define the deleteNode function of  the  binary search tree Sample Input40 20 60 10 30 50 70 5 15 25 35 45 55 65 75 -1 Sample Output:5 15 10 25 35 30 20 45 55 50 65 75 70 60 40Sample input40 20 60 10 30 50 70 5 15 25 35 45 55 65 75 -1 60Sample outputoutput=5 15 10 25 35 30 20 45 55 50 65 75 70 4

Is Balanced TreeGiven an array of unique elements, construct a Binary Search Tree and check if its balanced. A tree is said to be balanced if for every node, the difference between the height of its child nodes is not greater than 1.Input FormatFirst line of each test case contains N - number of nodes in the BST. The next line contains N unique integers - value of the nodes.Constraints1 <= N <= 10000 <= ar[i] <= 10000Output FormatFor each test case, print "Yes" if the Binary Search Tree is balanced, "No" otherwise, separated by newline.Sample input6 50 30 70 20 40 60Sample outputYes

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.