not_founded
not_founded

Reputation: 51

Find the level of array elements in BST

We have a given array and we want to print each node's level in the BST.

For example if the given array is: {15, 6, 2, 10, 9, 7, 13}

then the answer is:

1 2 3 3 4 5 4

(it means that the level of node that stores 15 is 1 and ...)

I have some algorithms in my mind but I don't know how to implement them in code.

Upvotes: 0

Views: 44

Answers (1)

Himanshu Singh
Himanshu Singh

Reputation: 2165

These are the steps that you should follow :

  1. Create a binary search tree from the elements given in the array.
  2. Write a function findLevel( Node root, int value) to find the level of any value passed to this function.
  3. Iterate the array and pass each array element as an argument to findLevel( Node root, int value) and print the values returned from the function.

Upvotes: 1

Related Questions